0

我如何将 php 的 json 内容传递给“the_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"));
      }
4

1 回答 1

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();

并继续您的代码..

如果这不是您的意思,请忽略。

于 2013-11-07T08:08:11.863 回答