问题描述:

Am trying to using org.apache.http.legacy library for asynctask

compileSdkVersion 23

buildToolsVersion "23.0.2"

dependencies {

compile fileTree(dir: 'libs', include: ['*.jar'])

compile 'com.android.support:appcompat-v7:23.1.1'

}

I heard that in API 23 "org.apache.http" is not working

so i tried this

android {

compileSdkVersion 23

buildToolsVersion '23.0.2'

useLibrary 'org.apache.http.legacy'

}

After that I sync the project , but no result

Still this much of library is not importing properly

import org.apache.http.Header;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.HttpClient;

import org.apache.http.client.ResponseHandler;

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

import org.apache.http.impl.client.BasicResponseHandler;

import org.apache.http.impl.client.DefaultHttpClient;

This problem arise when I change to API Level 21 to API Level 23. Because i want to work with API level 23

This is the method that am using for parsing

 new AsyncHttpClient().get("url",

new TextHttpResponseHandler() {

@Override

public void onStart() {

}

@Override

public void onRetry(int retryNo) {

}

@Override

public void onFailure(int arg0, Header[] arg1,

String response, Throwable arg3) {

}

@Override

public void onSuccess(int arg0, Header[] arg1,

String response) {

}

网友答案:

As org.apache.http.client.HttpClient was deprecated in API level 22 and is no longer supported in API level 23, Please use openConnection(). Please see the code below.

URL url = new URL("urlString");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");   
InputStream is = new BufferedInputStream(conn.getInputStream());
String response = org.apache.commons.io.IOUtils.toString(is, "UTF-8");
System.out.println(response);
网友答案:

It was said that a org.apache.http.legacy.jar in Android/sdk/platforms/android-23/optional,

So we can add that library in out application lib folder after that use useLibrary 'org.apache.http.legacy'

and then sync it

相关阅读:
Top