简体   繁体   中英

Android JSONObject always returns “null”

I'm trying to parse JSON from my web server. Fetching data isn't the problem but when I try to create a JSONObject it always returns null .

My JSON:

{"apikey":"c34750f19843vo45239o","error":false}

And my Java:

private class GetJSON extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        JSONResult = null;
        HttpHandler sh = new HttpHandler();
        sh.HTTPMethod = DefaultMethod;

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(ApiUrl+tmpUrl);

        System.out.println("Response from "+ApiUrl+tmpUrl+": " + jsonStr);

        if (jsonStr != null) {
            try {
                JSONResult = new JSONObject(jsonStr);

            } catch (final JSONException e) {
                System.out.println("Json parsing error: " + e.getMessage());
            }
        } else {
            System.out.println("Couldn't get json from server.");
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        clearRequest();
    }

}

I have tried with jsonStr.toString() but same result. I can also log my JSON string in Java but it isn't parsed.

This is where it returns null :

if(JSONResult != null && JSONResult.has("apikey")) {
    ApiKey = JSONResult.getString("apikey");
    System.out.println("Successfully got ApiKey: "+ApiKey);
}else{
    if(JSONResult == null){System.out.println("Is Null");}
}

Sorry for my bad English. Hope you understand :)

Just guessing, since you did not show enough of your code.

Most likely you check the result before it has been received. The AsyncTask runs asynchronously to your main thread and you would have to wait until it is finished before you can use the result.

In fact, that's the reason why there is an onPostExecute method. This is the correct place to process the result.

you need to initialize JSONObject JSONResult = null; in your doInBackground method

   @Override
    protected Void doInBackground(Void... arg0) {
        JSONObject JSONResult = null;
        HttpHandler sh = new HttpHandler();
        sh.HTTPMethod = DefaultMethod;

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(ApiUrl+tmpUrl);

        System.out.println("Response from "+ApiUrl+tmpUrl+": " + jsonStr);

        if (jsonStr != null) {
            try {
                JSONResult = new JSONObject(jsonStr);

            } catch (final JSONException e) {
                System.out.println("Json parsing error: " + e.getMessage());
            }
        } else {
            System.out.println("Couldn't get json from server.");
        }

        return null;
    }

Try this

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