[英]json parsing function returns weird package name
I have made a JSON parsing class which returns a string. 我做了一个JSON解析类,它返回一个字符串。 when i set the text of a textview to the string, it print 'com.test.app.JSONParser@41eddbf8'
当我将textview的文本设置为字符串时,它将输出'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? 由于AsyncTask是一个单独的类,如何使OnPostExecute()的结果进入主要活动?
to return the json string to the calling fragment so i could manipulate it from there. 将json字符串返回到调用片段,以便我可以从那里进行操作。
EDIT 2: 编辑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.
因此,对于其他任何有此问题的人,您需要做的就是在调用结束时添加.get()。
So: String jsonString = new JSONParser().execute(jsonURL).get();
因此:
String jsonString = new JSONParser().execute(jsonURL).get();
was my call and it worked! 是我的电话,它奏效了! I didnt even need a onPostExecute block in my asynctask.
我什至在我的asynctask中甚至不需要onPostExecute块。
The doInBackground
method does not return its result when you call execute, instead the result is passed to the onPostExecute
method. 当您调用execute时,
doInBackground
方法不会返回其结果,而是onPostExecute
结果传递给onPostExecute
方法。 It looks like you are using an AsyncTask
incorrectly. 看来您使用的
AsyncTask
不正确。 Usually you'd do something with the result in onPostExecute
that needs the main thread. 通常,您需要使用
onPostExecute
中的结果来做一些需要主线程的事情。
What you are seeing is the toString
output on the JSONParser
class, since an AsyncTask
will return this
when you call execute
. 您所看到的是
JSONParser
类上的toString
输出,因为当您调用execute
时, AsyncTask
将返回this
。
You are printing an object whose toString
method is most likely not overriden to print a pretty output. 您正在打印其
toString
方法很可能不会被覆盖以打印漂亮输出的对象。
If JSONParser
is your own class, then override the toString method to print the output in a desired way. 如果
JSONParser
是您自己的类,则重写toString方法以所需的方式输出输出。 If JSONParse is not your class then you need to find how to print the output properly from the api doc. 如果JSONParse不是您的课程,那么您需要找到如何正确打印api文档中的输出的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.