I have created an application where I had extended an Activity and got the response from the Web Service Successfully using the Asynctask, Now I want to do the Json Parsing of that received Response in the another class other than in the main activity.
我已经创建了一个应用程序,我已经扩展了一个Activity并从Web服务获得了响应使用Asynctask成功获得了响应,现在我想在除主活动之外的另一个类中对收到的响应进行Json解析。
How can do that? How do I use the received Response of the Main Activity in a separate Class because in the next class I just want to do the Parsing using the Response achieved from the Main Class.
怎么办?如何在单独的类中使用接收到的主活动的响应,因为在下一个类中,我只想使用从主类中获得的响应进行解析。
Can anybody please give me an answer of this!
谁能请给我一个答案!
Thanks, david
2 个解决方案
#1
1
You can sent value to another class by passing to its constructor, or passing as any function arguments.
您可以通过传递给它的构造函数或者传递任何函数参数来将值发送到另一个类。
Or if you want to pass value to Asynctask class
或者,如果要将值传递给Asynctask类
public class abc extends Activity {
new AsynctaskClassName.execute(response);
}
And the Asynctask will be
Asynctask将是
public class AsynctaskClassNameextends AsyncTask <String, String, String> {
protected Z doInBackground(String... res){
...
response = res[0]
...
}
Or to pass value to another intent, its better to pass by
或者将值传递给另一个意图,最好通过
Intent.putExtra("response", response);
and read by
并阅读
Bundle extras = getIntent().getExtras();
String response = extras.getString("response");
#2
0
You can simply create a new class
您只需创建一个新类
public class Parser
{
public static String rawJSON;
....
}
Now in your AsyncTask simply store JSON response in Parser.rawJSON like this
现在在你的AsyncTask中,只需像这样在Parser.rawJSON中存储JSON响应
protected Z doInBackground(X...x){
...
Parser.rawJSON=responseFromServer(URL);
...
}
& you are done!
你完成了!
#1
1
You can sent value to another class by passing to its constructor, or passing as any function arguments.
您可以通过传递给它的构造函数或者传递任何函数参数来将值发送到另一个类。
Or if you want to pass value to Asynctask class
或者,如果要将值传递给Asynctask类
public class abc extends Activity {
new AsynctaskClassName.execute(response);
}
And the Asynctask will be
Asynctask将是
public class AsynctaskClassNameextends AsyncTask <String, String, String> {
protected Z doInBackground(String... res){
...
response = res[0]
...
}
Or to pass value to another intent, its better to pass by
或者将值传递给另一个意图,最好通过
Intent.putExtra("response", response);
and read by
并阅读
Bundle extras = getIntent().getExtras();
String response = extras.getString("response");
#2
0
You can simply create a new class
您只需创建一个新类
public class Parser
{
public static String rawJSON;
....
}
Now in your AsyncTask simply store JSON response in Parser.rawJSON like this
现在在你的AsyncTask中,只需像这样在Parser.rawJSON中存储JSON响应
protected Z doInBackground(X...x){
...
Parser.rawJSON=responseFromServer(URL);
...
}
& you are done!
你完成了!