compiling error on org.apache.http

Ebad Ahmed picture Ebad Ahmed · Oct 20, 2015 · Viewed 15.1k times · Source

i have build an app to insert a record but it give error on compiling kindly help me my jasonparser class is

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

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

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.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) 

Answer

Jozua picture Jozua · Oct 20, 2015

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.

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