Question

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.

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

Was it helpful?

Solution

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

 public class abc extends Activity {

 new AsynctaskClassName.execute(response);      
 }

And the Asynctask will be

 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");

OTHER TIPS

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

protected Z doInBackground(X...x){
...
Parser.rawJSON=responseFromServer(URL);
...
}

& you are done!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top