android - Reading json from java application with python -


I am working on a project with my colleague. We have a dragon api, jquery web interface + java android interface In fact, the Web and Android should send data in Jason format and my API should read it and process it. Python has no problem with reading the variables from the request of the post from jquery through CGI. Here's the snippet (without headers and accessories) that works just fine:

  Importing CGI storage = CGFildStorage () Print (storage)  
< P> Return value is (as expected):

  fieldstodge (none, none, [minifined storage ('command', 'xi'), minifield storage ('additional ',' 168 ')])  

With it, I can easily call

  storage ["command"]  

Unfortunately, when sending a single request from Java, I did not get the expected results. At this time we use a snippet.

  HTTP client httpclient = new DefaultHttpClient (); Http post httppost = new HTTP post (LOGIN_URL); JSONObject json = faucet; {Json = New JSONObject ("{\" command \ ": \" xy \ ", \" extra \ ": \" 168 \ "}); From = new string (json.toString (), "UTF-8"); Se.setContentType ("Application / Jason; Charset = UTF-8"); Httppost.setEntity (se); Httppost.setHeader ("content-type", "app / json"); HttpResponse response = httpclient.execute (httppost); Bufferreader Reader = New buffed reader (new interstitial reader). Antita (). GetContent (), "UTF-8"); String jsonString = null; While ((jsonString = reader.readLine ()) = null) {Log.i ("feedback", jsonString); } Return tap; } Catch (client protocol reception e) {e.printStackTrace ();} Catch (IOE EXPRESS E) {e.printStackTrace ();} Catch (JasonXExtension) {e.printStackTrace ();}   
  field storage (none, none, '{"command": "xy", "when reading this request (print) in Python, You can see that I have received the string, this is not a MiniFieldStorage example and I can not read it.  

  storage.list  

No one else except, I can not do it again over storage (TypeError, "is not indexable") < / P>

We are trying to solve it for hours and we are still not sure where the problem is - whether its python / CGI or Java (but I feel a strong feeling that we have to go wrong Code because none of us is a real Java codec)

Note for non-Python coders: I believe that the problem is that Java sends a string instead of json object, but I can also be wrong - me pg.cgi

< P>

You do not show how your data is being posted from the "web" version, but it does not appear that you post JSON here Are: You are only sending normal form data with "command" and "extra" fields.

But the Java version is is sending JSON (your point about "sending a string" is irrelevant: the way the JSON data has been transferred).

Therefore, since you are not sending form-encoded data, cgi.FieldStorage is irrelevant and you should read raw data only from sys.stdin , Then decode it:

  imported data = sys.stdin.read () parameters = json.loads (data)  

Comments

Popular posts from this blog

Member with no value in F# -

java - Joda Time Interval Not returning what I expect -

c# - Showing a SelectedItem's Property -