java - org.apache.http 编译错误

标签 java android apache-httpcomponents

<分区>

我已经构建了一个应用程序来插入记录 但它在编译时出错请帮助我 我的 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;
    }
}

它在导入库时出错 导入 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中加入后 它在调试时出错 应用程序正在打开,然后不幸地给出错误应用程序关闭

请检查一下谢谢 我的日志猫是

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) 

最佳答案

您的目标是 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

关于java - org.apache.http 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33234675/

相关文章:

java - Android View 需要 API 级别 14

java - Android: ListView 单元格不显示所选的休闲字体

java - 尝试使用关键监听器,但形状不会移动

android - 为什么 FirebaseMessagingService.onMessageReceived() 没有被调用?

android - 在 android 中使用 rest api 和解析数据、jackson 或 groovy 的最佳方法是什么?

java - 如何从响应中查找 HTTP 媒体类型(MIME 类型)?

java - 使用 ImageJ 进行图像分割

android - 如何查看我的 Android 应用的网络消耗?

java - 使用 DefaultHttpResponseWriter 编写实体

Java8 Apache Http 客户端 4.5.12 - SocketException