0

I am fetching URL from below URL but i got following error

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

The following is logcat error

       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)

but if i use another url like http://api.androidhive.info/contacts/ it is working fine

i have used POST method but still NULL is returning

     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();
        } 
Siddhpura Amit
  • 14,534
  • 16
  • 91
  • 150
  • 3
    Which line is line 40 in your snippet? – mthmulders Apr 17 '13 at 10:41
  • What's the line giving the NPE? (i.e. line 40 of your JSONParser class) – Matt Taylor Apr 17 '13 at 10:42
  • Have you modified anything in the PHP script. check whether it is properly returning the json response you need. – SKK Apr 17 '13 at 10:43
  • Why do you use `getJSONFromURL()` when the next moment you contact the server yourself? If you really need to `POST`, the `getJSONFromUrl()` may not get a reasonable answer. – class stacker Apr 17 '13 at 10:43
  • *Always* check the HTTP response status (with `httpResponse.getStatusLine().getStatusCode()`) before trying to read it's data. You may very well be bombing on `is = httpEntity.getContent()`, assuming you've shown all your code. – Perception Apr 17 '13 at 10:45
  • Funny, you were editing your code as I was making my comment! Ok, you need to check your response status, looks like your server is returning you an error, and there is no entity to read data from, hence the NPE. – Perception Apr 17 '13 at 10:48
  • Hello Perception just check that link on your browser it is returning value – Siddhpura Amit Apr 17 '13 at 10:50
  • Do not know but if i use last url it is working fine – Siddhpura Amit Apr 17 '13 at 10:51
  • http://stackoverflow.com/questions/9679673/how-do-i-use-the-command-httpentity , http://stackoverflow.com/questions/10684086/getting-the-response-body-of-httpresponse – rajeshwaran Apr 17 '13 at 10:53
  • @SiddhpuraAmit: can you Saw us your full java file Source code here? – Bhavesh Patadiya Apr 17 '13 at 11:08
  • @SiddhpuraAmit - you should be using a GET for the request, not a POST (POST's to it don't return any data, hence the NPE). Also, move the status line check to ***before*** you read the response. – Perception Apr 17 '13 at 11:24

1 Answers1

1

The URL in question does not support posts (you can test that yourself with any kind of REST client). But, as you yourself have indicated, it does work with GET's. So switch up your code to something like as shown below and problem solved:

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

You need to add back in (of course) some more advanced error handling

Perception
  • 79,279
  • 19
  • 185
  • 195