Friday, 9 August 2013

json parsing function returns weird package name

json parsing function returns weird package name

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.

No comments:

Post a Comment