0

我有 PHP 网络服务,我想把它叫做 throw AsyncTask..在这个阶段我没有问题..我的问题是我想让一个类扩展AsyncTask并传递给它uri,以便在许多活动中多次webservices调用它,但我不知道该怎么做并调用 asynctask .. 任何帮助.. 谢谢

这是我试图做的,但它不起作用

    public class GetNetworkData  extends AsyncTask<String, Void, String>{

    String uri;
    private OnPostExecuteListener  mPostExecuteListener = null;
    public static interface OnPostExecuteListener
    {

                void onPostExecute(String result);

            }


    GetNetworkData(
            String Url,
            OnPostExecuteListener postExecuteListener) throws Exception {

            uri = Url;
            mPostExecuteListener = postExecuteListener;
            if (mPostExecuteListener == null)
                throw new Exception("Param cannot be null.");
        }

    @Override
    protected String  doInBackground(String... params) {
        // TODO Auto-generated method stub  

        JSONObject param=null;
      String result = null;
      HttpClient httpClient = new DefaultHttpClient();
            HttpHost httpHost = new HttpHost("192.168.3.111",80);  
            HttpPost httpPost = new HttpPost(uri);
            httpPost.addHeader("Content-Type", "application/json; charset=utf-8");


            try
            {
                //HttpEntity bodyEntity = new StringEntity(param.toString(), "utf8");
                //httpPost.setEntity(bodyEntity);
                HttpResponse response = httpClient.execute(httpPost);
                HttpEntity entity = response.getEntity();

                if (entity != null) {
                    InputStream instream = entity.getContent();
                    BufferedReader reader = new BufferedReader(
                         new InputStreamReader(instream));
                    StringBuilder sb = new StringBuilder();


                    String line = null;
                    while ((line = reader.readLine()) != null)
                        sb.append(line + "\n");


                    result = sb.toString();
                    instream.close();      
                }

                }
                catch (Exception e) {
                    // TODO: handle exception
                }

        return result;
    }   

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
         if (mPostExecuteListener != null)
         {
          try {
                    JSONObject json = new JSONObject(result);
                    mPostExecuteListener.onPostExecute(result);
                } catch (JSONException e){
                    e.printStackTrace();
                }
            }           
    }  
}
4

2 回答 2

0

GetNetworkData随时随地创建类实例。如果所有调用的Url和参数都相同,您可以考虑一个包装类,它将这些保存在实例变量中并按需创建对象。postExecuteListenerGetNetworkData

使用的代码GetNetworkData是:

String url = "http://....";
GetNetworkData.OnPostExecuteListener listener = new GetNetworkData.OnPostExecuteListener() {
    @Override
    public void onPostExecute(String result) {
        Log.d("AsyncTask", "onPostExecute: " + result); 
        //Do what you want to do after the task is complete
    }
};

GetNetworkData task = new GetNetworkData(url, listener);
task.execute();
于 2012-04-18T12:51:59.117 回答
0

打电话做它喜欢,

String url = "so and so url";
new GetNetworkData().execute(url);

在你的 doInBackground() 中,

@Override  
protected String  doInBackground(String... params) {  
    // TODO Auto-generated method stub  
    urlToUse = params[0];     
}

每次执行AsyncTask..它都会去doInBackgroud()执行里面的代码..在代码执行完成后..它会自动进入onPostExecute()AsyncTask的生命周期。我认为..您不需要为此做任何事情..对于 AsyncTask 的每个不同调用。

于 2012-04-18T12:56:22.837 回答