如何在MainActivity类Android

时间:2015-05-22 07:06:13

标签: android

我制作了一个GetJSONTask类,扩展了AsynkTaskClass。在onPostExecute方法中,如果我在onCreate中传递单个网址,我会得到结果。像

 newCustomAsync().execute("http://abcd.com/fetch/Service1.svc/GetStates?"+
                    "CCode=" +country+ "");

我想调用多个URL并在不同的变量中检索多个JSON。

private class CustomAsync extends AsyncTask<String, Void, String> {

    protected void onPreExecute() {
        super.onPreExecute(); 
     }  
    @Override
    protected String doInBackground(String... URL) {

            HttpClient Client = new DefaultHttpClient();
           try{
            String SetServerString = "";
            HttpGet httpget = new HttpGet(URL[0]);
            ResponseHandler<String> responseHandler =
            new BasicResponseHandler();
            SetServerString = Client.execute(httpget, responseHandler);

            JSONArray myListsAll= new JSONArray(SetServerString);
            for(int i=0;i<myListsAll.length();i++){
                JSONObject jsonobject= (JSONObject) myListsAll.get(i);


                 String states = jsonobject.optString("states");
                    }
                    return SetServerString;
            }catch(Exception e){}
        return null;
     }

       protected void onPostExecute(String result) {
            super.onPostExecute(result);
            JSONArray myListsAll = null;
            try {
                myListsAll = new JSONArray(result);
            }   catch (JSONException e) {

                e.printStackTrace();
            }


            JSONObject jsonobject= null;
            try {
                jsonobject = (JSONObject) myListsAll.get(0);
            } catch (JSONException e) {

                e.printStackTrace();
            }

           String msg =jsonobject.optString("states");
        }
 }

1 个答案:

答案 0 :(得分:0)

调用doInbackground()中的所有url并获取结果。要将多个结果传递给onPostExecute(),请使用包装类,您必须传递包装类的对象。

public class CustomAsync extends AsyncTask<String, Integer, Wrapper> {

    @Override
    protected Wrapper doInBackground(String... params) {
        String url1 = params[0];
        String url2 = params[1]
        Wrapper wrapper = new Wrapper();
        wrapper.result1 = Get(url1);
        wrapper.result2 = Get(url2);
        return wrapper;
    }

    @Override
    protected void onPostExecute(Wrapper wrapper) {

        String result1 = wrapper.result1;
        String result2 = wrapper.result2;
        // Do your operation
    }

    public class Wrapper {
        public String result1;
        public String result2;

    }

}

将Get()定义为单独的函数。代码更干净

public static synchronized String Get(String url){

    HttpClient Client = new DefaultHttpClient();
    try{
        String SetServerString = "";
        HttpGet httpget = new HttpGet(url);
        ResponseHandler<String> responseHandler =
        new BasicResponseHandler();
        SetServerString = Client.execute(httpget, responseHandler);
        JSONArray myListsAll= new JSONArray(SetServerString);
        for(int i=0;i<myListsAll.length();i++){
            JSONObject jsonobject= (JSONObject) myListsAll.get(i);
            String states = jsonobject.optString("states");
        }
        return SetServerString;
    }catch(Exception e){}
    return null;

}