Bouncing between “Adapter is detached” and “No wrapped connection” with HttpClient

后端 未结 4 1747
囚心锁ツ
囚心锁ツ 2021-01-07 05:26

So as i said i\'m bouncing back and forth between these two errors when trying to run HttpClient.execute(HttpPost). Getting IllegalStateException

4条回答
  •  天涯浪人
    2021-01-07 05:56

    Try with this..

    // Execute the asynctask with your URL

    new myAsyncTask().execute("http://example.com/ExampleFolder/");
    

    // Asynctask Callback method

    private class myAsyncTask extends AsyncTask
        {
            protected void onPreExecute()
            {
                super.onPreExecute();
            }
            @Override
            protected Void doInBackground(String... arg0)
            {
                // Creating service handler class instance
                ServiceHandler serhand = new ServiceHandler();
                // Making a request to url and getting response
                serhand.makeServiceCall(arg0[0], ServiceHandler.GET);
    
                return null;
            }
            protected void onPostExecute(Void result)
            {
            }
        }
    

    // Service Handler class

    package yourpagename
    
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.util.List;
    
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.client.utils.URLEncodedUtils;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.util.EntityUtils;
    
    public class ServiceHandler {
    
        static String response = null;
        public final static int GET = 1;
        public final static int POST = 2;
    
        public ServiceHandler() {
    
        }
    
        /**
         * Making service call
         * @url - url to make request
         * @method - http request method
         * */
        public String makeServiceCall(String url, int method) {
            return this.makeServiceCall(url, method, null);
        }
    
        /**
         * Making service call
         * @url - url to make request
         * @method - http request method
         * @params - http request params
         * */
        public String makeServiceCall(String url, int method,
                                      List params) {
            try {
                // http client
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpEntity httpEntity = null;
                HttpResponse httpResponse = null;
    
                // Checking http request method type
                if (method == POST) {
                    HttpPost httpPost = new HttpPost(url);
                    // adding post params
                    if (params != null) {
                        httpPost.setEntity(new UrlEncodedFormEntity(params));
                    }
    
                    httpResponse = httpClient.execute(httpPost);
    
                } else if (method == GET) {
                    // appending params to url
                    if (params != null) {
                        String paramString = URLEncodedUtils
                                .format(params, "utf-8");
                        url += "?" + paramString;
                    }
                    HttpGet httpGet = new HttpGet(url);
    
                    httpResponse = httpClient.execute(httpGet);
    
                }
                httpEntity = httpResponse.getEntity();
                response = EntityUtils.toString(httpEntity);
    
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            return response;
    
        }
    }
    

提交回复
热议问题