Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You cannot run network operation on the main thread.</p> <p>Use <a href="http://developer.android.com/reference/android/os/AsyncTask.html" rel="nofollow">asyncTask</a> instead.</p> <pre><code>public class JSONExampleActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); new GetSmsTask().execute("http://iatek.eu/sys/getsms.php"); } private class GetSmsTask extends AsyncTask&lt;String, Void, JSONObject&gt; { protected JSONObject doInBackground(String... urls) { JSONObject obj = null; try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url[0]); HttpResponse response = httpclient.execute(httppost); String jsonResult = inputStreamToString(response.getEntity() .getContent()).toString(); obj = new JSONObject(jsonResult); } catch (JSONException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return obj; } protected void onPostExecute(JSONObject obj) { JSONArray jsonArray = obj.getJSONArray("posts"); JSONObject childJSONObject = jsonArray.getJSONObject(3); String username = childJSONObject.getString("username"); String sms = childJSONObject.getString("sms"); String fcat = childJSONObject.getString("fcat"); textView.setText(""+sms+"--" + username); } } } </code></pre> <p>EDIT:</p> <p>I just ran the code myslef, and I got this </p> <blockquote> <p>09-13 13:33:59.315: W/System.err(14200): org.json.JSONException: Unterminated object at character 7551 of {"posts":[{......</p> </blockquote> <p>This means your JSON is invalid</p> <p><a href="http://pro.jsonlint.com/" rel="nofollow">http://pro.jsonlint.com/</a> paste your link there you will see the error.</p> <p>To test that the code is working replace this</p> <pre><code>String jsonResult = inputStreamToString(response.getEntity() .getContent()).toString(); </code></pre> <p>with this:</p> <pre><code>String jsonResult = "{\"posts\":" + "[{\"cid\":\"11\",\"username\":\"Livi\",\"sms\":\"ag\",\"fto\":\"\",\"fcat\":\"cat\"}," + " {\"cid\":\"11\",\"username\":\"Livi\",\"sms\":\"ag\",\"fto\":\"\",\"fcat\":\"cat\"}, " + "{\"cid\":\"11\",\"username\":\"Livi\",\"sms\":\"ag\",\"fto\":\"\",\"fcat\":\"cat\"}," + "{\"cid\":\"11\",\"username\":\"Livi\",\"sms\":\"ag\",\"fto\":\"\",\"fcat\":\"cat\"}]}"; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload