我已经构建了一个应用程序来插入记录 但它编译错误,请帮助我 我的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;
}
}
它在导入库上出错 import org.apache.http
错误窗口是
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.
请事先帮助我
在build.gradle中添加后它给出了调试错误 应用程序正在打开,然后它给出了错误的appfor关闭
请检查一下谢谢 我的日志猫是
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.access$700(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.access$700(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)
答案 0 :(得分:8)
你的目标是MarshMallow吗?请检查您的build.gradle
文件。如果它包含minSdkVersion=23
,那么您还必须在构建文件中包含以下内容。
android {
useLibrary 'org.apache.http.legacy'
}
不推荐使用Apache库,并将其从Android MarshMallow中的Android SDK中删除。在此处详细了解:https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html
答案 1 :(得分:4)
使用&org.apache.http.client&#39;时遇到了类似的问题。在带有Gradle的Android Studio中。如果您使用的是最新的构建工具,即SDK 23,那么此库将被折旧。
要解决这个问题,你可以用另一种方式包含它,它应该仍然适用于最新的构建工具/ SDK:
dependencies {
compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
}
你也可以参考这个答案: Can't import org.apache.http.HttpResponse in Android Studio