Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从给出NullPointerException的url获取json_Java_Android_Http - Fatal编程技术网

Java 从给出NullPointerException的url获取json

Java 从给出NullPointerException的url获取json,java,android,http,Java,Android,Http,我正在从下面的URL获取URL,但出现以下错误 url = "http://qrpromenad.azurewebsites.net/api/question?id=1&imei=35404304"; public JSONObject getJSONFromUrl(String url) { System.out.println(url); try { // defaultHttpClient DefaultHttpClie

我正在从下面的URL获取URL,但出现以下错误

 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)
但是如果我使用另一个url,就像它工作得很好一样

我使用了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();
        } 
DefaultHttpClient-httpClient=newdefaulthttpclient();
HttpPost HttpPost=新的HttpPost(Const.POST_URL);
//添加您的数据
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“id”,“1”));
添加(新的BasicNameValuePair(“imei”、“123456”);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse HttpResponse=httpClient.execute(httpPost);
HttpEntity HttpEntity=httpResponse.getEntity();
System.out.println(“httpEntity”+httpEntity);//这里是空的
如果(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){//false
is=httpEntity.getContent();
} 

所讨论的URL不支持POST(您可以使用任何类型的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);

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

代码片段中的第40行是哪一行?给出NPE的那一行是什么?(即JSONParser类的第40行)您是否修改了PHP脚本中的任何内容。检查它是否正确返回了所需的json响应。下一次您自己联系服务器时,为什么要使用
getJSONFromURL()
?如果确实需要发布,
getJSONFromUrl()
可能无法得到合理的答案。在尝试读取HTTP响应数据之前,请始终检查HTTP响应状态(使用
httpResponse.getStatusLine().getStatusCode()
)。假设您已经展示了所有的代码,您很可能正在轰炸
is=httpEntity.getContent()
。非常感谢:)