无法在AsyncTask android中的onPostExcecute中设置值为字符串

时间:2016-06-06 15:01:12

标签: java android android-asynctask

这是从web

读取json字符串的类
  {

  public class JSONmethod extends AsyncTask<String,String,String>
  {

   public  String result_string;

@Override
protected void onPreExecute() {
    super.onPreExecute();

}

@Override
protected String doInBackground(String... params) {
    BufferedReader reader = null;
    HttpURLConnection connection = null;
    StringBuffer buffer;
    try {
        URL url;
        url = new URL(params[0]);
        connection = (HttpURLConnection) url.openConnection();
        connection.connect();
        InputStream stream = connection.getInputStream();
        reader = new BufferedReader(new InputStreamReader(stream));
        String line= "";
        buffer = new StringBuffer();
        while ((line = reader.readLine())!= null)
        {
            buffer.append(line);
        }
        return buffer.toString();

    }

    catch(MalformedURLException e)
    {
        e.printStackTrace();
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }
    finally
    {
        if (connection != null) {
            connection.disconnect();
        }
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;

}

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);


    result_string=result;

}



public String result_string_josn()
{
    return result_string;
}
 }

方法&#34; result_string_json()&#34; return null string

我想经常使用这个类来从web上读取json字符串

所以我为返回字符串创建了这个方法,它将从onPostExecute返回

这是我想要通过方法或其他任何东西在后执行中生成的值的类

simple.java

 package com.bhatti.bis;

 import android.app.Activity;
 import android.os.Bundle;
 import android.widget.Toast;


public class simple extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.simple);

    JSONmethod j = new JSONmethod();
    j.execute("here is json string");


    Toast.makeText(this,j.result_string_josn(),Toast.LENGTH_LONG).show();
}

}

3 个答案:

答案 0 :(得分:0)

异步任务是异步任务,请阅读https://en.wikipedia.org/wiki/Asynchrony_%28computer_programming%29

在以下后删除Toast

JSONmethod j = new JSONmethod();
j.execute("here is json string");

并将其放入onPostExecute:

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);
    Toast.makeText(this,result,Toast.LENGTH_LONG).show();
}

答案 1 :(得分:0)

Android使用单个用户界面(UI)线程处理输入事件/任务,该线程称为主线程。主线程无法处理并发操作,因为它一次只处理一个事件/操作。对于detail read this tutorials

JSONmethod jSONmethod = new JSONmethod();
jSONmethod.execute("Your json string");



public class JSONmethod extends AsyncTask<String,String,String>
  {

   public  String result_string;

@Override
protected void onPreExecute() {
    super.onPreExecute();

}

@Override
protected String doInBackground(String... params) {
    BufferedReader reader = null;
    HttpURLConnection connection = null;
    StringBuffer buffer;
    try {
        URL url;
        url = new URL(params[0]);
        connection = (HttpURLConnection) url.openConnection();
        connection.connect();
        InputStream stream = connection.getInputStream();
        reader = new BufferedReader(new InputStreamReader(stream));
        String line= "";
        buffer = new StringBuffer();
        while ((line = reader.readLine())!= null)
        {
            buffer.append(line);
        }
        return buffer.toString();

    }

    catch(MalformedURLException e)
    {
        e.printStackTrace();
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }
    finally
    {
        if (connection != null) {
            connection.disconnect();
        }
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;

}

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);


  Log.d("JSONmethod","result = "+result);

}


 }

答案 2 :(得分:0)

使用EventBus库。它非常易于使用,可以完美解决您的问题:

首先为您的活动创建一个简单的类: 公共类MyEvent {     私有字符串数据;

public MyEvent(String data) {
    this.data = data;
}

public String getData() {
    return data;
}

public void setData(String data) {
    this.data = data;
}

然后在您的活动或任何地方,注册并取消注册EventBus,如文档中所述。

现在发布相应的活动:

   @Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);

    EventBus.getDefault().post(new MyEvent(jsonArray.toString()));
}

所有留给你的是在任何你想要的地方聆听那个事件(在另一个活动,片段,服务中 - 这就是使EventBus变得更棒的原因):

@Subscribe
public void onMyEvent(MyEvent myEvent){
    String data = myEvent.getData();
    //do whatever you wish with the text (e.g. make a toast, write it somewhere)
}