2

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

public class NetMethods {
    private static HttpClient client = new DefaultHttpClient();

    public static void getStuff() {

    ArrayList<Alarm> alarms = new ArrayList<Alarm>();

    HttpPost post = HttpPostFactory.getHttpPost("GetStuff");

    StringBuilder builder = new StringBuilder();

    HttpResponse response = client.execute(post); // Exception thrown here

            ...

Also, my MttpPostFactory just has this

import org.apache.http.client.methods.HttpPost;

public class HttpPostFactory {

private static final String url = "http://example.com/ExampleFolder/";

public static HttpPost getHttpPost(String s) {
    return new HttpPost(url + s);
}
}
Tarsem Singh
  • 13,979
  • 7
  • 50
  • 70
JaySee
  • 341
  • 3
  • 14
  • did you tried `HttpPost post = new HttpPost("http://example.com/ExampleFolder/GetStuff");` giving same exception or working ? – Tarsem Singh Aug 23 '13 at 04:55

4 Answers4

9

Try this,

BasicHttpParams params = new BasicHttpParams();
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
final SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
DefaultHttpClient httpclient = new DefaultHttpClient(cm, params);
shiju B
  • 1,690
  • 10
  • 23
4

This may arise from not closing the InputStream's you get from HttpClient, especially if arising from different threads...either not reading the whole content or calling the same HttpClient instance from two different threads.

Pratik Butani
  • 56,749
  • 54
  • 254
  • 407
kwishnu
  • 1,592
  • 17
  • 16
0

I found solution from Androider's blog:

I got the log :

Invalid use of SingleClientConnManager: connection still allocated.

or

Caused by: java.lang.IllegalStateException: No wrapped connection.

or

Caused by: java.lang.IllegalStateException: Adapter is detached.

Finally got Solution:

public static DefaultHttpClient getThreadSafeClient() {

       DefaultHttpClient client = new DefaultHttpClient();

       ClientConnectionManager mgr = client.getConnectionManager();

       HttpParams params = client.getParams();

       client = new DefaultHttpClient(new ThreadSafeClientConnManager(params,

              mgr.getSchemeRegistry()), params);

       return client;

}
Pratik Butani
  • 56,749
  • 54
  • 254
  • 407
0

Try with this..

// Execute the asynctask with your URL

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

// Asynctask Callback method

private class myAsyncTask extends AsyncTask<String, Void, Void>
    {
        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<NameValuePair> 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;

    }
}
Amitabha Biswas
  • 3,271
  • 1
  • 10
  • 22