我正在尝试将 org.apache.http.legacy 库用于asynctask
compileSdkVersion 23
buildToolsVersion "23.0.2"
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.1.1'
}
我听说在API 23中“org.apache.http”无效 所以我试过这个
android {
compileSdkVersion 23
buildToolsVersion '23.0.2'
useLibrary 'org.apache.http.legacy'
}
之后我同步了项目,但没有结果
仍然有很多图书馆没有正确导入
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;
当我将API Level 21更改为API Level 23时会出现此问题。因为我想使用API级别23
这是用于解析的方法
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) {
}
答案 0 :(得分:1)
由于org.apache.http.client.HttpClient
已在API级别22中弃用,并且在API级别23中不再受支持,请使用openConnection()
。请参阅下面的代码。
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);
答案 1 :(得分:0)
有人说在Android / sdk / platforms / android-23 / optional中有一个org.apache.http.legacy.jar,
因此我们可以在应用程序lib文件夹中添加该库
之后使用
useLibrary 'org.apache.http.legacy'
然后同步