简体   繁体   中英

android Error parsing data org.json.JSONException

I have two URLs.

  1. https://wimp.supremosolutions.com/api/v1/spot/?format=json
  2. https://wimp.supremosolutions.com/api/v1/spot/?format=json&latitude=45.200375&longitude=9.992465000000001

If I run the first one through my android activity then the results load into my List with no problem. But if I run the second URL then I land up getting this error.

05-30 16:20:30.473: ERROR/JSON Parser(1740): Error parsing data org.json.JSONException: Value Invalid of type java.lang.String cannot be converted to JSONObject
05-30 16:20:30.633: ERROR/AndroidRuntime(1740): FATAL EXCEPTION: AsyncTask #1
        java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:278)
        at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
        at java.util.concurrent.FutureTask.run(FutureTask.java:137)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
        at java.lang.Thread.run(Thread.java:856)
        Caused by: java.lang.NullPointerException
        at com.supremosolutions.wimp.AroundMeParking$LoadInbox.doInBackground(AroundMeParking.java:162)
        at com.supremosolutions.wimp.AroundMeParking$LoadInbox.doInBackground(AroundMeParking.java:138)
        at android.os.AsyncTask$2.call(AsyncTask.java:264)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
        ... 5 more   

I have run both URLs through json validators like http://jsonlint.com/ and both validate.

The code seems to break on the jsonParser.makeHttpRequest :

protected String doInBackground(String... args) {
    // Building Parameters
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    JSONObject json = jsonParser.makeHttpRequest(callableUrl, "GET", params);
    // Check your log cat for JSON response
    Log.d("JSON FOR LOGCAT: ", json.toString());

Sometimes I can see the JSON FOR LOGCAT string in Logcat and it looks like this http://pastebin.com/6CWBAyys (still crashes with that exception though)but sometimes the string doesn't even make it into logcat.

What is the problem here and how can I fix it?

The jsonParser class that I had appends parameters. I was already adding in the base url + the parameters and so the url being sent was invalid. Instead of sending my URL through like this https://wimp.supremosolutions.com/api/v1/spot/?format=json&latitude=45.200375&longitude=9.992465000000001

I have to send it like https://wimp.supremosolutions.com/api/v1/spot/ but then add NameValuePair parameters to pass into my jsonParser class.

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("format", "json"));
        params.add(new BasicNameValuePair("format", "json"));
        params.add(new BasicNameValuePair("format", "json"));
        JSONObject json = jsonParser.makeHttpRequest(URL, "GET", params);

As Ken YN and Alex mentioned this error is caused by an invalid url. If you get this DOUBLE check how you are creating your URL and see exactly what is being sent

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM