而不是静态地放置'the_json'的值我如何将php的json内容传递给'the_json'。这是我的代码
try
{
String the_json = "{'profiles': [{'name':'john', 'age': 44}, {'name':'Alex','age':11}]}";
JSONObject myjson = new JSONObject(the_json);
JSONArray the_json_array = myjson.getJSONArray("profiles");
int length = the_json_array.length();
for (int i = 0; i < length; i++)
{
JSONObject another_json_object = the_json_array.getJSONObject(i);
String artist_name = another_json_object.optString("name").toString();
models.add(new Model(R.drawable.ic_drawer, artist_name,"12"));
}
}
编辑:试过这个......什么都没发生
String url="http//10.0.2.2/jan/android/fave.php";
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
String the_json = reader.readLine();
JSONObject myjson = new JSONObject(the_json);
JSONArray the_json_array = myjson.getJSONArray("products");
int length = the_json_array.length();
for (int i = 0; i < length; i++)
{
JSONObject another_json_object = the_json_array.getJSONObject(i);
String artist_name = another_json_object.optString("title").toString();
models.add(new Model(R.drawable.ic_drawer, artist_name,"12"));
}
答案 0 :(得分:0)
不确定你的意思,如果你的php url给了一个json对象,你可以通过在android中使用httpget方法得到它
代码如下。
String url="http//"myurl.php";
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("name of header", "value of heder"));
HttpResponse response = null;
if(mHttpClient != null || !mHttpClient.equals(null) || !mHttpClient.equals(""))
{
response = mHttpClient.execute(httpGet);
}
else
{
mHttpClient = getClient();
response = mHttpClient.execute(httpGet);
}
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String the_json = reader.readLine();
并继续使用您的代码..
如果这不是您的意思,请忽略。