我有这个 HTTP/JSON 代码应该从数据库中检索数据,但是我该如何将它打印到屏幕上。如果您需要任何额外的代码,请告诉我。谢谢
String result = "";
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("name","Badminton"));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("myurl.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
}
catch(Exception e1){
Log.e("log_tag", "Error in http connection "+e1.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//parse json data
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++)
{
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","type: "+json_data.getInt("id"));
}
}
catch(Exception e)
{
Log.e("log_tag", "Error parsing data "+e.toString());
}
那么我将如何将我从 PHP 文件收到的数据打印到屏幕上,我稍后会用它做更多的事情