第一個問題。這樣如何在android中獲取json?
echo(json_encode($thing));
PHP回聲JSON數據,那麼我怎樣才能在Android的這個JSON數據?
我必須使用JSONArray,JSONObject嗎?
第二個問題。如果php echos字符串數據,那麼我怎麼能將這個字符串數據轉換爲JSON數據? 我嘗試在Android使用asynctask httpPost,它從PHP獲取字符串數據。
String get_url = null;
// TODO Auto-generated method stub
get_url = sendData("music", "http://www.test.com/test.php");
private String sendData(String name, String url)
throws ClientProtocolException, IOException {
// TODO Auto-generated method stub
HttpPost request = makeHttpPost(name, url);
HttpClient client = new DefaultHttpClient();
ResponseHandler<String> reshandler = new BasicResponseHandler();
String result = client.execute(request, reshandler);
return result;
}
private HttpPost makeHttpPost(String name, String url) {
// TODO Auto-generated method stub
HttpPost request = new HttpPost(url);
Vector<NameValuePair> nameValue = new Vector<NameValuePair>();
nameValue.add(new BasicNameValuePair("name", name));
request.setEntity(makeEntity(nameValue));
return request;
}
private HttpEntity makeEntity(Vector<NameValuePair> nameValue) {
HttpEntity result = null;
try {
result = new UrlEncodedFormEntity(nameValue);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
這裏是一個指南:http://www.vogella.com /articles/AndroidJSON/article.html –
請參閱下面的鏈接我已發佈http://stackoverflow.com/questions/20212520/android-json-parse-not-working/20212854#20212854(從URL讀取json數據) – prakash