Defaulthttpclient to Androidhttpclient

DefaultHttpClient to AndroidHttpClient

StrictMode.ThreadPolicy was introduced since API Level 9 and the default thread policy had been changed since API Level 11, which in short, does not allow network operation (include HttpClient and HttpUrlConnection) get executed on UI thread. if you do this, you get NetworkOnMainThreadException.

This restriction can be changed, using:

    if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}

Add the above code into your main activity's onCreate() method.

In addition, it is always recommended to move network operation off the UI thread, for example, using AsyncTask.

hope this help.

Android HttpClient, DefaultHttpClient, HttpPost

I think in your code the basic problem is caused by the way you are using StringEntity to POST parameters to your url. Check to see if the following code helps in posting your data to the server using StringEntity.

    // Build the JSON object to pass parameters
JSONObject jsonObj = new JSONObject();
jsonObj.put("username", username);
jsonObj.put("data", dataValue);

// Create the POST object and add the parameters
HttpPost httpPost = new HttpPost(url);
StringEntity entity = new StringEntity(jsonObj.toString(), HTTP.UTF_8);
entity.setContentType("application/json");
httpPost.setEntity(entity);

HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(httpPost);

Hope this helps in solving your problem. Thanks.

AndroidHttpClient and DefaultHttpClient

AndroidHttpClient: Subclass of the Apache DefaultHttpClient that is configured with reasonable default settings and registered schemes for Android, and also lets the user add HttpRequestInterceptor classes.
This client processes cookies but does not retain them by default. To retain cookies, simply add a cookie store to the HttpContext

[API]

Replacement for deprecated DefaultHttpClient

Ok I waited for a week and so and did all lots of researches. I think I have found the answer.

I strongly advise for beginners and even professional Android programmers to know the existence of very helpful library called Retrofit:

Extensive document is present at http://square.github.io/retrofit/

The Stack overflow also has samples for almost everything one need to do over a network to contact a remote REST service.

It is better to stop using HttpURLConnection and AsyncTask. Retrofit is way faster and supports all failure situations out of the box.

DefaultHttpClient deprecated

What you are doing is the thing of the past. Move on to google's own volley library to make network calls. It is super easy and you do not require AsyncTask anymore.

Just download the library from here and then import it as a module in your project. Simple.

Note: there is also a GsonRequest class in this library and it will set your model class with the data that you are getting. You do not even have to any parsing, so it saves a lot of time.

HttpClient won't import in Android Studio

HttpClient is not supported any more in sdk 23. You have to use URLConnection or downgrade to sdk 22 (compile 'com.android.support:appcompat-v7:22.2.0')

If you need sdk 23, add this to your gradle:

android {
useLibrary 'org.apache.http.legacy'
}

You also may try to download and include HttpClient jar directly into your project or use OkHttp instead

Do POST request with Android DefaultHTTPClient cause freeze on execute()

Use AndroidHttpClient helped me in this situation.

But now complete AndroidHttpClient and DefaultHttpClient are obsolete in current version of Android so it is not important now.

Android HttpClient : NetworkOnMainThreadException

On ICS and later you cannot do network operations on the UI thread anymore. Instead you are forced to create a new thread and do your networking stuff there.

Possible tools are Android's AsyncTask and the normal Java Thread.

A good tutorial can be found here: Android Threads, Handlers and AsyncTask - Tutorial



Related Topics



Leave a reply



Submit