简体   繁体   中英

how to return string from AsyncTask to another activity

I have created a class and extend with AsyncTask , now i want to return me a string to another activity throw this class . its getting jsonstring from mysql mDatabase

enter code here  

public class GetJson  extends AsyncTask<Void,Void,String> {
String  JSON_STRING;
String json_url;
public String pasString;
Activity ab;
@Override
protected void onPreExecute() {
    json_url="https://XXXXXXX.000webhostapp.com/Json_getData_Survey.php";
}

public GetJson(Activity b) {
    ab=b;
}

@Override
protected String doInBackground(Void... params) {

    try {
        URL url =new URL(json_url);
        HttpURLConnection httpURLConnection =(HttpURLConnection)  url.openConnection();
        InputStream inputStream  =httpURLConnection.getInputStream();
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder stringBuilder=new StringBuilder();

        while ((JSON_STRING=bufferedReader.readLine())!=null)
        {

            stringBuilder.append(JSON_STRING+"\n");
        }


        bufferedReader.close();
        inputStream.close();
        httpURLConnection.disconnect();

        String  strings =stringBuilder.toString().trim();
        return  strings;

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

@Override
protected void onProgressUpdate(Void... values) {
    super.onProgressUpdate(values);
}

@Override
protected void onPostExecute(String result) {

    pasString=result;

   // Toast.makeText(ab,pasString,Toast.LENGTH_LONG).show();
   // json_string1=result;
}

}

i just need to return string1 to my another Activity , i have creaetd a another mehtod in GetJson class to return this string but when i display this it show nothing in it. please help

its Quite simple make an interface class as following

public interface ResponseListener() {
      void onResponseReceive(String data);
}

and add instance of this class in you GetJson class like this

public class GetJson  extends AsyncTask<Void,Void,String> {
String  JSON_STRING;
String json_url;
public String pasString;
Activity ab;

// this is new code
ResponseListener listener;
public void setOnResponseListener(ResponseListener listener) {
    this.listener = listener;
}

@Override
protected void onPreExecute() {
    json_url="https://XXXXXXX.000webhostapp.com/Json_getData_Survey.php";
}

// now add this code in onPost function

@Override
protected void onPostExecute(String result) {

   pasString=result;
   listener.onResponseReceive(pasString);

  // Toast.makeText(ab,pasString,Toast.LENGTH_LONG).show();
  // json_string1=result;
}

now when you call GetJson from your activity, just simple do that;

GetJson json = new GetJson(Activity.this);
json.setOnResponseListener(new ResponseListener() {
       @Override
       public void onResponseReceived(String data) {
           // here you will get your response                                               
       }
});

hope you will understand. :)

Try using activity results. You can set the return value on the one activity, and get it when that activity terminates.

https://developer.android.com/training/basics/intents/result.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM