从url获取json,给出NullPointerException

时间:2013-04-17 10:40:15

标签: java android http

我从以下网址获取网址,但我收到了以下错误

 url = "http://qrpromenad.azurewebsites.net/api/question?id=1&imei=35404304";


    public JSONObject getJSONFromUrl(String url) {
    System.out.println(url);
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();  // here i got null value

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

以下是logcat错误

       04-17 16:00:12.739: W/System.err(4579): java.lang.NullPointerException
       04-17 16:00:12.739: W/System.err(4579):  at         com.qrcodetest.JSONParser.getJSONFromUrl(JSONParser.java:40)
       04-17 16:00:12.739: W/System.err(4579):  at com.qrcodetest.Result$FetchURL.doInBackground(Result.java:50)
       04-17 16:00:12.739: W/System.err(4579):  at com.qrcodetest.Result$FetchURL.doInBackground(Result.java:1)
       04-17 16:00:12.739: W/System.err(4579):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
       04-17 16:00:12.739: W/System.err(4579):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
       04-17 16:00:12.739: W/System.err(4579):  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
       04-17 16:00:12.739: W/System.err(4579):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
       04-17 16:00:12.739: W/System.err(4579):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
       04-17 16:00:12.739: W/System.err(4579):  at java.lang.Thread.run(Thread.java:1019)

但是,如果我使用其他网址http://api.androidhive.info/contacts/,那就可以了。

我使用过POST方法,但仍然返回NULL

     DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(Const.POST_URL);
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("id", "1"));
        nameValuePairs.add(new BasicNameValuePair("imei", "123456"));
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));


        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        System.out.println("httpEntity "+httpEntity); // here is null
        if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {// false
            is = httpEntity.getContent();
        } 

1 个答案:

答案 0 :(得分:1)

相关网址不支持帖子(您可以使用任何类型的REST客户端自行测试)。但是,正如你自己指出的那样,它确实适用于GET。因此,将代码切换为如下所示的问题并解决问题:

final String url = "http://qrpromenad.azurewebsites.net/api/question?id=1&imei=35404304";

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet get = new HttpGet(url);

HttpResponse httpResponse = httpClient.execute(get);

String responseData = null;
final int status = httpResponse.getStatusLine().getStatusCode();
if (status == HttpStatus.SC_OK) {
    responseData = EntityUtils.toString(httpResponse.getEntity());
} else {
    System.err.println("HTTP request failed with status " + status);
}

System.out.println(responseData);

您需要重新添加(当然)一些更高级的错误处理