0

通过以下代码,我无法解析来自 URL 的数据

try{

  HttpClient client= new DefaultHttpClient();

            HttpGet http= new HttpGet("http://api.androidhive.info/contacts/");

            HttpResponse response = client.execute(http);

            HttpEntity httpentity = response.getEntity();

            is= httpentity.getContent();

        }catch (ClientProtocolException e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
4

3 回答 3

1

由于您没有说明您不理解解析的哪一部分,您可以阅读本教程,该教程展示了如何解析与您完全相同的 JSON 数据:

http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

于 2013-09-28T11:30:42.303 回答
1

如果您尝试检索数据,则可以使用以下代码将响应转换为字符串值,

HttpClient client= new DefaultHttpClient();
HttpPost http=new HttpPost("http://api.androidhive.info/contacts/");
HttpResponse response = client.execute(http);
HttpEntity httpentity = response.getEntity();
String result=EntityUtils.toString(httpentity);

使用此结果值解析 JSON。您可以使用org.json jar 文件,其中包含JSONArray的构造函数,其中String作为参数。

例如。JSON解析

JSONArray jarray=new JSONArray(result);
for(int i=0;i<jarray.length();i++)
{
   JSONObject jobject=jarray.getJSONObject(i);
   System.out.println(jobject.getString("test"));
}

在您的情况下,您需要解析第一个 JSONObject 而不是 JSONArray。

于 2013-09-28T11:34:31.190 回答
0

这样做..这是我在许多应用程序中使用从 url 获取 json 响应的工作代码。

 /**
 * This method used to get json from url.
 * 
 * @param url
 *            represented url
 * @return represented {@link JSONObject}
 */
public JSONObject getJSONFromUrl(String url) {

    InputStream is = null;
    JSONObject jObj = null;
    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        HttpResponse httpResponse = httpClient.execute(httpGet);

        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        jObj = new JSONObject(getResponseBody(is));

    } catch (Exception e) {
        e.printStackTrace();
    }
    return jObj;
}

/**
 * This method used to get response body.
 * 
 * @param instream
 *            represented input stream
 * @return represented {@link String}
 * @throws IOException
 *             represented {@link IOException}
 * @throws ParseException
 *             represented {@link ParseException}
 */
public String getResponseBody(final InputStream instream) throws IOException, ParseException {

    if (instream == null) {
        return "";
    }

    StringBuilder buffer = new StringBuilder();

    BufferedReader reader = new BufferedReader(new InputStreamReader(instream, "utf-8"));

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            buffer.append(line);
        }

    } finally {
        instream.close();
        reader.close();
    }
    System.out.println(buffer.toString());
    return buffer.toString();

}
于 2013-09-28T11:32:29.757 回答