mysql - Android: Error Converting string to JSONObject -


I am trying to retrieve data from my database as JSON, and with that result JSONObject Which then sets up a Textview.

Here is the code:

  Private class ShowDBActivity3 AsyncTask expands & lt; Zero, zero, string & gt; {Secure string doInBackground (zero ... voids) {string = ""; ArrayList & LT; NameValuePair & gt; NameValuePairs = New ArrayList & lt; NameValuePair & gt; (); NameValuePairs.add (New BasicNameValuePair ("username", user name)); {HttpClient httpclient = Try New DefaultHttpClient (); Http post httppost = new HTTP post ("url"); Httppost.setEntity (new UrlEncodedFormEntity (nameValuePairs)); HttpResponse response = httpclient.execute (httppost); HttpEntity unit = response.getEntity (); Is = entity.getContent (); Logs. E ("pass 1", "connection success"); } Hold (exception e) {Log.e ("failed 1", etsting ()); Toast. Make Text (getApplicationContext), "Invalid IP Address", Toast.LNNHH_LOG). Show (); } {BufferedReader Reader = Try New BufferedReader (New InputStreamReader ("iso-8859-1"), 8); Stringbilder sb = new stringbiller (); While ((line = reader. Readline ())! = Null () {sb.append (row + "\ n"); }             is close(); Result = sb.toString (); Logs. ("Pass 2", "connection success"); } Hold (exception e) {Log.e ("unsuccessful 2", E. T. Sting ()); } {Log. Try it ("This is the result:", result. To string ()); JSONObject jArray = new JSONObject (results); String title = jArray.getString ("title"); String rating = jArray.getString ("Rating"); String Username = jArray.getString ("Username"); S = "Movie:" + Title + "\ n" + "Rating:" + Rating + "\ n"; Return s; } Hold (exception e) {Log.e ("unsuccessful 3", E. T. Sting ()); } Return S; } Stroke on safe exclamation (strings) {show.setText (s); And the result is:  

The result is: [["0": "16", "id": "16", " "7", "3": "tlong3", "username": "" Incredible Hulk " "3", "3": "tlong3", "user": "1", "1": "19", "1" rating "," Incredible Hulk "," Incredible Hulk "," 2 ":" 3 "," 1 " "" ":" "" "": "" "" ":" "" "" "": "" "" ":" 6 "," id ":" 20 "," 1 ":" Testtt "" title ":" Test "," 2 ":" 6 " ":" 6 "," 3 ":" Tlong3 "," Username ":" Tlong3 "}]

... which can not be changed in JSONObject for any reason. Here's the error:

04-28 13: 14: 25.372 2009 9-20830 / com Android Media / E3 failed: org.json.JSONException: value [{"" 3: "tlong3" "hook", "1": "hulk", "0": "16": "16", "2": "7", "user name": "tlong3", "id": "19", "tlong3", "title": "hulk", "1", "title": "7"}, {"3" "Incredible Hulk", "1": "Incredible Hulk", "0": "19", "Rating": "3", "user name": "tlong3", "title": "Incredible Hulk": " "", "Title": "Tlong3", "id": "20", "2" "6", "Username": "Talge 3", "Title": "Test", "1": " JSONObject

This is obvious and the LockAt also shows that JSONArray JSONObjec T can not be converted into a string that you are receiving as a result is a JSONArray and the object is not, you are trying to parse it JSONObjct which is the cause of the error.

This is for you if you do not already know JSONArray can be identified with [your JSONArray] and JSONObject is {your JSONObject}

You can use the tool to verify and validate the JSON response. According to its format, JSONresponse [{***} {***} {***}) which means that you need JSONObjects to parse JSONArray properly, you need to change it in the first JSON Apply for loop to go through Array and then through each of its objects. I have answered such a question and, you should check them and try it the same way. PS: If you are developing server scripts, then I recommend that you give the server side code so that your feedback is always JSONObject instead of JSONArray but only if When you are the developer of both the client and server, if you need help, then ask them questions


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -