如何在android中恢复和停止web服务asynctask

时间:2015-08-18 10:23:35

标签: android

我有列表视图,我在其中放置使用Web服务asynctask在服务器中的图像。图像被放置。每当我尝试点击图像上的列表视图时,它就会卡在模拟器和手机中..

    WebServiceTask wst = new WebServiceTask(WebServiceTask.GET_TASK, this, "Loading.....");
        wst.execute(new String[]{sampleURL});

 private class WebServiceTask extends AsyncTask<String, Integer, String> {

        public static final int POST_TASK = 1;
        public static final int GET_TASK = 2;
        private static final String TAG = "WebServiceTask";
        // connection timeout, in milliseconds (waiting to connect)
        private static final int CONN_TIMEOUT = 3000;
        // socket timeout, in milliseconds (waiting for data)
        private static final int SOCKET_TIMEOUT = 5000;
        private int taskType = GET_TASK;
        private Context mContext = null;
        private String processMessage = "Processing...";

        private ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();

        private ProgressDialog pDlg = null;

        public WebServiceTask(int taskType, Context mContext, String processMessage) {

            this.taskType = taskType;
            this.mContext = mContext;
            this.processMessage = processMessage;
        }

        public void addNameValuePair(String name, String value) {

            params.add(new BasicNameValuePair(name, value));
        }

        private void showProgressDialog() {

            pDlg = new ProgressDialog(mContext);
            pDlg.setMessage(processMessage);
            pDlg.setProgressDrawable(mContext.getWallpaper());
            pDlg.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            pDlg.setCancelable(false);
            pDlg.show();

        }

        @Override
        protected void onPreExecute() {
            //hideKeyboard();
            showProgressDialog();
        }

        protected String doInBackground(String... urls) {

            String url = urls[0];
            String result = "";

            HttpResponse response = doResponse(url);

            if (response == null) {
                return result;
            } else {

                try {

                    result = inputStreamToString(response.getEntity().getContent());

                } catch (IllegalStateException e) {
                    Log.e(TAG, e.getLocalizedMessage(), e);

                } catch (IOException e) {
                    Log.e(TAG, e.getLocalizedMessage(), e);
                }

            }

            return result;
        }

        @Override
        protected void onPostExecute(String response) {


            handleResponse(response);
            //aa.clear();

            pDlg.dismiss();

        }

        // Establish connection and socket (data retrieval) timeouts
        private HttpParams getHttpParams() {

            HttpParams htpp = new BasicHttpParams();

            HttpConnectionParams.setConnectionTimeout(htpp, CONN_TIMEOUT);
            HttpConnectionParams.setSoTimeout(htpp, SOCKET_TIMEOUT);

            return htpp;
        }

        private HttpResponse doResponse(String url) {

            // Use our connection and data timeouts as parameters for our
            // DefaultHttpClient
            HttpClient httpclient = new DefaultHttpClient(getHttpParams());

            HttpResponse response = null;

            try {
                switch (taskType) {

                    case POST_TASK:
                        HttpPost httppost = new HttpPost(url);
                        //httppost.setHeader("Authorization","" );
                        //httppost.addHeader("Authorization", apikey);
                        httppost.setEntity(new UrlEncodedFormEntity(params));

                        response = httpclient.execute(httppost);
                        break;
                    case GET_TASK:
                        HttpGet httpget = new HttpGet(url);
                        //httpget.addHeader("Authorization", apikey);
                        //httpget.setHeader("Authorization",apikey );
                        response = httpclient.execute(httpget);
                        break;
                }
            } catch (Exception e) {

                Log.e(TAG, e.getLocalizedMessage(), e);

            }

            return response;
        }

    public void handleResponse(String response) {


        try {
            JSONObject jsonObject=new JSONObject(response);

            JSONArray jso = jsonObject.getJSONArray("search_result");

            if(jso.length() != 0) {

                for (int i = 0; i < jso.length(); i++) {

                    Person resultRow = new Person();
                    resultRow.id = jso.getJSONObject(i).getString("property_id");
                    resultRow.ProjectName = jso.getJSONObject(i).getString("property_name");

                    JSONObject ppty_image_object = jso.getJSONObject(i);
                    JSONArray ppty_image_array = ppty_image_object.getJSONArray("property_image");
                    for (int j = 0; j < ppty_image_array.length(); j++) {

                        resultRow.logo =ppty_image_array.getJSONObject(j).getString("image");

                      //  resultRow.logo[j] = ppty_image_array.getJSONObject(j).getString("image");


                    }
                  //  resultRow.logo =jso.getJSONObject(i).getString("image");
                    resultRow.id = jso.getJSONObject(i).getString("user_id");
                    resultRow.ProjectName = jso.getJSONObject(i).getString("property_name");
                    resultRow.bathrooms = jso.getJSONObject(i).getString("bathrooms");
                    resultRow.Host_Name = jso.getJSONObject(i).getString("user_name");

                    resultRow.city = jso.getJSONObject(i).getString("city");
                    resultRow.price = jso.getJSONObject(i).getString("price");
                    resultRow.bed = jso.getJSONObject(i).getString("bed");
                    resultRow.bedrooms = jso.getJSONObject(i).getString("bedrooms");
                    resultRow.guest_allow = jso.getJSONObject(i).getString("guest_allow");
                    resultRow.roomtype = jso.getJSONObject(i).getString("room_type");
                    resultRow.property_type = jso.getJSONObject(i).getString("property_type");


                    resultRow.user_ppty_id = jso.getJSONObject(i).getString("property_id");
                    resultRow.check_in_time = jso.getJSONObject(i).getString("check_in_time");
                    resultRow.check_out_time = jso.getJSONObject(i).getString("check_out_time");
                    if(jso.getJSONObject(i).getString("host_image").toString().equals("{}")){
                        resultRow.host_image= (getResources().getDrawable(R.drawable.close)).toString();
                    }else {
                        resultRow.host_image = jso.getJSONObject(i).getString("host_image");
                    }
                    ImageAdapter adapter = new ImageAdapter(this);

                    resultRow.city = jso.getJSONObject(i).getString("city");
                    resultRow.descrip = jso.getJSONObject(i).getString("description");
                    resultRow.latitude=jso.getJSONObject(i).getString("latitude");
                    resultRow.longlati=jso.optJSONObject(i).getString("longitude");
                    resultRow.minstay=jso.getJSONObject(i).getString("minimum_stay");
                    resultRow.cancelation=jso.getJSONObject(i).getString("cancellation_policy");
                    resultRow.houserules=jso.getJSONObject(i).getString("house_rules");

                    // resultRow.logo=jso.getJSONObject(i).getString("logo");
                    arrayOfwebData.add(resultRow);
                }

            }
        }
        catch (NullPointerException np){
            Log.e(TAG, np.getLocalizedMessage(), np);
        }
        catch (Exception e) {
            Log.e(TAG, e.getLocalizedMessage(), e);
        }
        ListView myListView = (ListView)findViewById(R.id.listview);

        aa=new FancyAdapter();
        myListView.setAdapter(aa);


        //   ViewHolder aa1= new ViewHolder();

    }

我想停止并恢复后台运行过程,每当我尝试点击列表视图行时,它将重定向到下一页,每当我按下后退按钮时,后台将恢复... 有人帮帮我。

0 个答案:

没有答案