public void getData() { String result = ""; InputStream isr = null; try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://10.0.0.2/androidDb/read.php"); //YOUR PHP SCRIPT ADDRESS HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); isr = entity.getContent(); Log.e("pass 1", "connection success "); } catch (Exception e) { Log.e("log_tag", "Error in http connection " + e.toString()); nameView.setText("Couldnt connect to database"); } //convert response to string try { BufferedReader reader = new BufferedReader(new InputStreamReader(isr, HTTP.UTF_8), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } isr.close(); result = sb.toString(); } catch (Exception e) { Log.e("log_tag", "Error converting result " + e.toString()); } //parse json data try { String s = ""; JSONArray jarray = new JSONArray(result); for(int i=0; i<jarray.length(); i++) { JSONObject json = jarray.getJSONObject(i); s = s + "ID " +json.getInt("ID")+"\nName: "+json.getString("Name")+"\n\n"; } nameView.setText(s); } catch (Exception e) { Log.e("log_tag", "Error Parsing Data " + e.toString()); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)