我有這個應該從數據庫檢索數據的HTTP/JSON代碼,但我該如何將它打印到屏幕上。如果您需要任何額外的代碼,請告訴我。由於將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文件到屏幕上接收到的數據,我會做更多的東西與它後來
你想在屏幕上顯示哪裏? ListView,GridView,TextView或者其他? – 2013-04-05 05:16:39
此刻的任何地方啊哈 – Ryaller 2013-04-12 12:42:45