Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I heard that starting from ICS, it is not allowed to perform network operations on UI (main) thread. Therefore, you must need to perform this operation in a separate thread. So either use <strong>AsyncTask</strong>, <strong>Thread</strong> or <strong>HandlerThread <em>with Looper and Handler</em></strong> to perform web operation. If you try it on a device running froyo or gingerbread, it would be fine but not on ICS.</p> <p>Regarding your attempt to run it via handler or AsyncTask, it depends on how you did it. Paste that code so i see the problem.</p> <hr> <p><strong>Update</strong></p> <pre><code>public class MySQLAndroidActivity extends ListActivity { private static final String url = "http://X.X.X.X/test.php"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity2); LoadTask t = new LoadTask(); t.execute(null); } private static class LoadTask extends AsyncTask&lt;Void, Void, Void&gt; { @Override protected void onPreExecute() { } @Override protected Void doInBackground(Void... params) { doStuff(); return null; } } public static void doStuff() { JSONArray jArray; String result = null; InputStream is = null; StringBuilder sb = null; HttpResponse response; HttpEntity entity; ArrayList&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(); // http post try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); response = httpclient.execute(httppost); entity = response.getEntity(); is = entity.getContent(); } catch (Exception e) { Log.e("log_tag", "Error in http connection" + e.toString()); System.exit(1); } // convert response to string try { BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); sb = new StringBuilder(); sb.append(reader.readLine() + "\n"); String line; while ((line = reader.readLine()) != null) { sb.append(line); } is.close(); result = sb.toString(); Log.i("json string", result); } catch (Exception e) { Log.e("log_tag", "Error converting result " + e.toString()); } // parsing data String idfriend; String id1; String id2; try { jArray = new JSONArray(result); JSONObject json_data = null; System.out.println("Length " + jArray.length()); Log.d("DB", "Length " + jArray.length()); for (int i = 0; i &lt; jArray.length(); i++) { System.out.println("counter " + i); json_data = jArray.getJSONObject(i); idfriend = json_data.getString("idfriend"); id1 = json_data.getString("id1"); id2 = json_data.getString("id2"); System.out.println("id1: " + id1); } } catch (JSONException e1) { Log.d("DB", "Error somewhere"); } catch (ParseException e1) { e1.printStackTrace(); } } } </code></pre> <p>*There were some method calling issues and the most important was that you were fetching keys out from JSONObject as <strong>Idfriend, Id1, Id2</strong> which is wrong because in your json string the keys are like <strong>idfriend, id1, id2</strong>. Its case-sensitive, thats why it was crashing.</p>
 

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