如何在AsyncTask android中获取字符串结果

时间:2015-11-17 06:29:06

标签: android android-asynctask

我有一个类AsyncCallWS,可以从webservice获取内容。它运作良好。但是,我希望在AsyncCallWS中获得结果,即returnServer中的MainActivity字符串。你能帮我解决一下吗?

 public class MainActivity extends Activity  {
    String resultRegister;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button btnRegister =(Button) findViewById(R.id.btnRegister);
    btnRegister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View v) {
            new AsyncCallWS().execute("123");
            Log.d("DDD",resultRegister);
            if(resultRegister.equals("")) {

                Log.d("D", "OK");
            }
            else
            {
                Log.d("E", "False");
            }
        }
    });

 }

private class AsyncCallWS extends AsyncTask<String, Void, Void> {
        @Override
        protected Void doInBackground(String... params) {
            Log.i(TAG, "doInBackground");
            String id_num = params[0];

            //toast(id);
            String url_registerID="server path"+id_num ;

            try {

                String returnServer=getStringContent(id);
                Log.d("D",returnServer);

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            Log.i(TAG, "onPostExecute");
        }

        @Override
        protected void onPreExecute() {
            Log.i(TAG, "onPreExecute");
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            Log.i(TAG, "onProgressUpdate");
        }

    }

2 个答案:

答案 0 :(得分:2)

从此Void

中删除第三个参数AsyncTask<String, Void, Void>

并将其替换为String,即

private class AsyncCallWS extends AsyncTask<String, Void, String>

更改后,您的doInBackground()会出现编译错误..只需将返回类型从Void更改为String

protected String doInBackground(String... params)

现在,您可以在onPostExecute(String result)

中获取此方法返回的字符串

这里的String resultdoInBackground()

返回的字符串

*编辑*

public class MainActivity extends Activity  {

String resultInActivity;

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  new AsyncCallWS().execute("123");
  //How to get respond from AsyncCallWS
 }

private class AsyncCallWS extends AsyncTask<String, Void, Void> {
    @Override
    protected String doInBackground(String... params) {
        Log.i(TAG, "doInBackground");
        String id_num = params[0];

        //toast(id);
        String url_registerID="server path"+id_num ;

        try {

            String returnServer=getStringContent(id);
            Log.d("D",returnServer);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return returnServer;
    }

    @Override
    protected void onPostExecute(String result) {
        Log.i(TAG, "onPostExecute");
        resultInActivity = result; 

        if(resultRegister.equals("")) {

            Log.d("D", "OK");
        }
        else
        {
            Log.d("E", "False");
        } 

    }

    @Override
    protected void onPreExecute() {
        Log.i(TAG, "onPreExecute");
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        Log.i(TAG, "onProgressUpdate");
     }

  } // Asyntask ends

} // activity ends

AsyncTask的响应来自onPostExecute(),您可以根据响应等执行任务。

我强烈建议您阅读AsyncTask Life Cycle

答案 1 :(得分:0)

私有类AsyncCallWS扩展了AsyncTask

用String替换第三个参数,结果doInBackground返回String并将相应的postExecute方法参数作为String

protected String doInBackground(String... params)

protected void onPostExecute(String result) 

为了在MainActivity上获取它,您必须执行以下步骤

  1. 在任意位置创建一个接口,并声明一个任何名称应该具有String类型参数的方法

    interface ResponseHandler 
     {
     void onResponse(String result)
     } 
    
  2. 在MainActivity中,您必须实现该界面 通过此方法获取数据

    ResponseHandler handler = new ResponseHandler(){
      @override
      public void onResponse(String result)
       {
         // enter code here
      }
      };
    
  3. 实现此接口后,您可以将其实例提供给AsyncTask构造函数。

     AsyncTask task = new AsyncTask(handler);
    
  4. 您将在AsyncTask中获得ResponseHandler的引用

       public AsyncTaskClass(ResponseHandler handler)
    
         {
        this.handler = handler
         }
    
    
         //enter code here
    
        void onPostExecute(String result)
    
        {
    
       handler.onResponse(result)
        }
    
  5. 这样您就可以在MainActivity中获得结果。