【问题标题】:compiling error on org.apache.http [duplicate]org.apache.http 上的编译错误 [重复]
【发布时间】:2015-10-20 10:50:45
【问题描述】:

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

【问题讨论】:

    标签: java android apache-httpcomponents


    【解决方案1】:

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

    【讨论】:

    • 我现在这样做了新的错误请检查它我正在更新我的问题
    • 这是一个新错误,与您的主要问题无关。如果您需要帮助,请打开一个新问题。至于新错误,请检查 MainActivity.java 文件中的第 36 行。您在这里使用 EditText,它会崩溃,因为您的布局中的视图不是 EditText,而是 TextView。
    【解决方案2】:

    在带有 Gradle 的 Android Studio 中使用“org.apache.http.client”时,我遇到了类似的问题。如果您使用的是最新的构建工具,即 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

    【讨论】:

      猜你喜欢
      • 2018-08-25
      • 2014-08-28
      • 2014-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-11
      • 2010-10-27
      相关资源
      最近更新 更多