java org.apache.http 上的编译错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33234675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
compiling error on org.apache.http
提问by Ebad Ahmed
i have build an app to insert a record but it give error on compiling kindly help me my jasonparser class is
我已经构建了一个应用程序来插入一条记录,但它在编译时出错,请帮助我我的 jasonparser 类是
import android.util.Log;
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.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
public JSONParser() {}
public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params) {
try {
if (method == "POST") {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} else if (method == "GET") {
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return jObj;
}
}
it gives error on import library import org.apache.http
它在导入库导入 org.apache.http 时出错
error window is
错误窗口是
E:\Users\Ahmed\AndroidStudioProjects\MyApplication\app\src\main\java\com\example\ahmed\myapplication\JSONParser.java
Error:(5, 23) error: package org.apache.http does not exist
Error:(6, 23) error: package org.apache.http does not exist
Error:(7, 23) error: package org.apache.http does not exist
Error:(31, 71) error: cannot find symbol class NameValuePair
Error:(38, 21) error: cannot access StringEntity
class file for org.apache.http.entity.StringEntity not found
Error:(39, 13) error: cannot find symbol class HttpResponse
Error:(39, 51) error: cannot access AbstractHttpMessage
class file for org.apache.http.message.AbstractHttpMessage not found
Error:(40, 13) error: cannot find symbol class HttpEntity
Error:(47, 13) error: cannot find symbol class HttpResponse
Error:(47, 51) error: cannot access HttpRequest
class file for org.apache.http.HttpRequest not found
Error:(48, 13) error: cannot find symbol class HttpEntity
E:\Users\Ahmed\AndroidStudioProjects\MyApplication\app\src\main\java\com\example\ahmed\myapplication\MainActivity.java
Error:(14, 23) error: cannot find symbol class NameValuePair
Error:(15, 31) error: package org.apache.http.message does not exist
Error:(71, 18) error: cannot find symbol class NameValuePair
Error:(71, 56) error: cannot find symbol class NameValuePair
Error:(72, 28) error: cannot find symbol class BasicNameValuePair
Error:(73, 28) error: cannot find symbol class BasicNameValuePair
Note: E:\Users\Ahmed\AndroidStudioProjects\MyApplication\app\src\main\java\com\example\ahmed\myapplication\JSONParser.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
19 warnings
Error:Execution failed for task ':app:compileDebugJava'.
> Compilation failed; see the compiler error output for details.
kindly help me thanks in advance
请帮助我提前致谢
after adding in build.gradle it give error on debug app is opening then it give error unfornately app close
在添加 build.gradle 后,它会在调试应用程序打开时出错,然后它会在应用程序关闭时出错
kindly check it thanks my log cat is
请检查一下,谢谢我的原木猫
10-21 23:24:00.100 16098-16098/com.example.ahmed.myapplication E/Trace: error opening trace file: No such file or directory (2)
10-21 23:24:00.327 16098-16098/com.example.ahmed.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
10-21 23:24:00.327 16098-16098/com.example.ahmed.myapplication E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ahmed.myapplication/com.example.ahmed.myapplication.MainActivity}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
10-21 23:24:00.327 16098-16098/com.example.ahmed.myapplication E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2136)
10-21 23:24:00.327 16098-16098/com.example.ahmed.myapplication E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2174)
10-21 23:24:00.327 16098-16098/com.example.ahmed.myapplication E/AndroidRuntime: at android.app.ActivityThread.access0(ActivityThread.java:141)
10-21 23:24:00.327 16098-16098/com.example.ahmed.myapplication E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1267)
10-21 23:24:00.327 16098-16098/com.example.ahmed.myapplication E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
10-21 23:24:00.327 16098-16098/com.example.ahmed.myapplication E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
10-21 23:24:00.327 16098-16098/com.example.ahmed.myapplication E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5059)
10-21 23:24:00.327 16098-16098/com.example.ahmed.myapplication E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
10-21 23:24:00.327 16098-16098/com.example.ahmed.myapplication E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
10-21 23:24:00.327 16098-16098/com.example.ahmed.myapplication E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
10-21 23:24:00.327 16098-16098/com.example.ahmed.myapplication E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
10-21 23:24:00.327 16098-16098/com.example.ahmed.myapplication E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
10-21 23:24:00.327 16098-16098/com.example.ahmed.myapplication E/AndroidRuntime: Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
10-21 23:24:00.327 16098-16098/com.example.ahmed.myapplication E/AndroidRuntime: at com.example.ahmed.myapplication.MainActivity.onCreate(MainActivity.java:36)
10-21 23:24:00.327 16098-16098/com.example.ahmed.myapplication E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5058)
10-21 23:24:00.327 16098-16098/com.example.ahmed.myapplication E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-21 23:24:00.327 16098-16098/com.example.ahmed.myapplication E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
10-21 23:24:00.327 16098-16098/com.example.ahmed.myapplication E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2174)?
10-21 23:24:00.327 16098-16098/com.example.ahmed.myapplication E/AndroidRuntime: at android.app.ActivityThread.access0(ActivityThread.java:141)?
10-21 23:24:00.327 16098-16098/com.example.ahmed.myapplication E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1267)?
10-21 23:24:00.327 16098-16098/com.example.ahmed.myapplication E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)?
10-21 23:24:00.327 16098-16098/com.example.ahmed.myapplication E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)?
10-21 23:24:00.327 16098-16098/com.example.ahmed.myapplication E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5059)?
10-21 23:24:00.327 16098-16098/com.example.ahmed.myapplication E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)?
10-21 23:24:00.327 16098-16098/com.example.ahmed.myapplication E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)?
10-21 23:24:00.327 16098-16098/com.example.ahmed.myapplication E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)?
10-21 23:24:00.327 16098-16098/com.example.ahmed.myapplication E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)?
10-21 23:24:00.327 16098-16098/com.example.ahmed.myapplication E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)?
回答by Jozua
Are you targetting MarshMallow? Please check your build.gradle
file. If it includes minSdkVersion=23
then you will have to also include the following in your build file.
您的目标是 MarshMallow 吗?请检查您的build.gradle
文件。如果包含,minSdkVersion=23
则您还必须在构建文件中包含以下内容。
android {
useLibrary 'org.apache.http.legacy'
}
The Apache libraries were deprecated and removed from the Android SDK in Android MarshMallow. Read more about this here: https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html
Apache 库已弃用并从 Android MarshMallow 的 Android SDK 中删除。在此处阅读更多相关信息:https: //developer.android.com/about/versions/marshmallow/android-6.0-changes.html
回答by WildStyle
I had a similar problem when using 'org.apache.http.client' in Android Studio with Gradle. If you're using the latest build tools i.e. SDK 23 then this library is depreciated.
在带有 Gradle 的 Android Studio 中使用“org.apache.http.client”时,我遇到了类似的问题。如果您使用的是最新的构建工具,即 SDK 23,则此库已贬值。
To get around this you can include it another way and it should still work with the latest build tools / SDK:
为了解决这个问题,你可以用另一种方式包含它,它应该仍然适用于最新的构建工具/SDK:
dependencies {
compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
}
You can also reference this answer: Can't import org.apache.http.HttpResponse in Android Studio
你也可以参考这个答案: Can't import org.apache.http.HttpResponse in Android Studio