0

I have made a JSON parsing class which returns a string. when i set the text of a textview to the string, it print 'com.test.app.JSONParser@41eddbf8'

What's going wrong?

Heres the parsing class

import android.os.AsyncTask;
import android.util.Log;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Created by Vaibhav on 8/9/13.
 */
public class JSONParser extends AsyncTask<String, Void, String>{
    protected String doInBackground(String... url) {
        DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
        HttpGet httpget = new HttpGet(url[0]);
        //httppost.setHeader("Content-type", "application/json");

        InputStream inputStream = null;
        String result;
        try {
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();

        inputStream = entity.getContent();
        // json is UTF-8 by default
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();

        String line;
        while ((line = reader.readLine()) != null)
        {
            sb.append(line + "\n");
        }
        result = sb.toString();
        return result;
        } catch (Exception e) {
        Log.d("com.test.app.JSONParser", "JSON ERROR:" + e.toString());
        return null;
    }
    finally {
        try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
    }
}
}

I call the function like

String jsonString = new JSONParser().execute(jsonURL).toString();

I dont know why im getting the weird response. Thanks

EDIT:

So since i wanted the output in a fragment, i just ended up using

How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?

to return the json string to the calling fragment so i could manipulate it from there.

EDIT 2:

OMG!! it was so easy. I wasted so much time on this! So for anyone else who has this problem, all you need to do is add .get() at the end of the call.

So: String jsonString = new JSONParser().execute(jsonURL).get(); was my call and it worked! I didnt even need a onPostExecute block in my asynctask.

Community
  • 1
  • 1
Vaibhav Aggarwal
  • 1,381
  • 2
  • 19
  • 30

2 Answers2

1

The doInBackground method does not return its result when you call execute, instead the result is passed to the onPostExecute method. It looks like you are using an AsyncTask incorrectly. Usually you'd do something with the result in onPostExecute that needs the main thread.

What you are seeing is the toString output on the JSONParser class, since an AsyncTask will return this when you call execute.

Abdullah Jibaly
  • 53,220
  • 42
  • 124
  • 197
  • Ok, so i have added the onPostExecute, but how do i make it return the string to the caller since its void? Thanks – Vaibhav Aggarwal Aug 10 '13 at 01:03
  • You can't, the purpose of an AsyncTask is to break the connection between the caller and the execution process. If you want to do this synchronously simply remove the code from inside the `AsyncTask` and call it from a regular function call. – Abdullah Jibaly Aug 10 '13 at 01:06
  • I was getting the `android.os.NetworkOnMainThreadException` before when it was a function i tried changing it to asynctask to try to fix it – Vaibhav Aggarwal Aug 10 '13 at 01:08
0

You are printing an object whose toString method is most likely not overriden to print a pretty output.

If JSONParser is your own class, then override the toString method to print the output in a desired way. If JSONParse is not your class then you need to find how to print the output properly from the api doc.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • its my class, the big chunk of code i have in the OP. But i think the problem is it isnt returning the response properly and is actually returning the function. Know how to fix it? – Vaibhav Aggarwal Aug 10 '13 at 00:57
  • @VaibhavAggarwal What is the return type of execute methods?Can you share your execute method code? – Juned Ahsan Aug 10 '13 at 00:59
  • `execute` is not his code, it's part of `AsyncTask` and it simply calls `doInBackground` on a different thread. – Abdullah Jibaly Aug 10 '13 at 01:04