HttpClient won't impor di Android Studio

Saya memiliki kelas sederhana yang ditulis dalam Android Studio:

package com.mysite.myapp;

import org.apache.http.client.HttpClient;

public class Whatever {
    public void headBangingAgainstTheWallExample () {
        HttpClient client = new DefaultHttpClient();
    }
}

dan dari ini saya dapatkan berikut compile time error:

Tidak bisa menyelesaikan simbol HttpClient

Isn't HttpClient termasuk dalam Android Studio SDK? Bahkan jika tidak, saya menambahkan ke saya Gradle membangun seperti ini:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.0.0'
    compile 'org.apache.httpcomponents:httpclient:4.5'
}

Dengan atau tanpa kompilasi terakhir line, kesalahan yang sama. Apa yang saya hilang?

Mengomentari pertanyaan (1)
Larutan

HttpClient tidak didukung lagi di sdk 23. Anda harus menggunakan URLConnection atau downgrade ke sdk 22 (compile 'com.android.dukungan:appcompat-v7:22.2.0')

Jika anda membutuhkan sdk 23, menambahkan ini untuk anda gradle:

android {
    useLibrary 'org.apache.http.legacy'
}

Anda juga dapat mencoba untuk men-download dan termasuk HttpClient jar langsung ke proyek anda atau gunakan OkHttp sebaliknya

Komentar (15)

HttpClient dipensiunkan dalam API Level 22 dan dihapus dalam API Level 23. Anda masih dapat menggunakannya dalam API Level 23 dan seterusnya jika anda harus, namun yang terbaik adalah untuk pindah ke metode yang didukung untuk menangani HTTP. Jadi, jika anda're kompilasi dengan 23, tambahkan ini di build.gradle:

android {
    useLibrary 'org.apache.http.legacy'
}
Komentar (11)

TejaDroid's jawaban di link di bawah ini membantu saya . https://stackoverflow.com/questions/32340629/cant-import-org-apache-http-httpresponse-in-android-studio

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:23.0.1'

    compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
    ...
}
Komentar (2)

Untuk menggunakan Apache HTTP untuk SDK Tingkat 23:

Top level membangun.gradle - /membangun.gradle

buildscript {
    ...
    dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0' 
        // Lowest version for useLibrary is 1.3.0
        // Android Studio will notify you about the latest stable version
        // See all versions: http://jcenter.bintray.com/com/android/tools/build/gradle/
    }
    ...
}

Pemberitahuan dari Android studio tentang gradle update:

Modul tertentu yang membangun.gradle - /app/build.gradle

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"
    ...
    useLibrary 'org.apache.http.legacy'
    ...
}
Komentar (0)

Coba ini bekerja untuk saya Tambahkan ketergantungan ini untuk membangun anda.gradle

compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
Komentar (0)

1 - download Apache file jar (sebagai jawaban ini) 4.5.zip file dari: https://hc.apache.org/downloads.cgi?Preferred=http%3A%2F%2Fapache.arvixe.com%2F

2 - buka zip copy file jar ke folder libs. Anda dapat menemukannya jika anda pergi ke bagian atas proyek di mana ia mengatakan "Android" anda'll menemukan daftar ketika anda klik. Jadi,

Android -> Proyek -> aplikasi> libs

,Kemudian dimasukkan ke dalam botol ada.

3 - Dalam membangun.gradle (Modul: aplikasi) tambahkan

compile fileTree(dir: 'libs', include: ['*.jar'])

di

 dependency { 
   }

4 - Di jawa kelas tambahkan impor ini:

import org.apache.http.HttpResponse;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.params.CoreProtocolPNames;
Komentar (0)

HttpClient tidak didukung lagi di sdk 23. Android 6.0 (API Level 23) rilis menghilangkan dukungan untuk Apache HTTP client. Anda harus menggunakan

android {
    useLibrary 'org.apache.http.legacy'
    .
    .
    .

dan juga tambahkan kode di bawah ini cuplikan dalam ketergantungan anda :

//http solusi akhir untuk layanan web (termasuk meng-upload file)

compile('org.apache.httpcomponents:httpmime:4.3.6') {
        exclude module: 'httpclient'
}
 compile 'org.apache.httpcomponents:httpclient-android:4.3.5'

Hal ini juga akan membantu anda saat anda gunakan MultipartEntity untuk meng-upload File.

Komentar (2)

di API 22 mereka menjadi usang dan API 23 mereka mereka benar-benar dihapus, solusi sederhana jika anda don't perlu semua barang-barang mewah dari tambahan baru adalah untuk hanya menggunakan .file jar dari apache yang terintegrasi sebelum API 22, tapi seperti yang dipisahkan .file jar:

1. http://hc.apache.org/downloads.cgi
2. download httpclient 4.5.1, the zile file
3. unzip all files
4. drag in your project httpclient-4.5.1.jar, httpcore-4.4.3.jar and httpmime-4.5.1.jar
5. project, right click, open module settings, app, dependencies, +, File dependency and add the 3 files
6. now everything should compile properly
Komentar (0)

Jika anda ingin mengimpor beberapa kelas seperti :

import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

Anda dapat menambahkan baris berikut dalam membangun.gradle (Gradle dependensi)

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.0'
    implementation 'com.android.support:support-v4:27.1.0'

    .
    .
    .

    implementation 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'

}
Komentar (2)

Anda hanya dapat menambahkan ini ke Gradle dependensi:

compile "org.apache.httpcomponents:httpcore:4.3.2"
Komentar (1)

Android 6.0 (API Level 23) rilis menghilangkan dukungan untuk Apache HTTP client. Oleh karena itu anda tidak dapat menggunakan perpustakaan ini langsung di API 23. Tapi ada cara untuk menggunakannya. Tambahkan useLibrary 'org.apache.http.warisan' dalam membangun.gradle file seperti di bawah ini-

android {
    useLibrary 'org.apache.http.legacy'
}

Jika ini tidak bekerja, anda mungkin berlaku berikut hack-

– Copy org.apache.http.legacy.jar yang di /platform/android-23/opsional path Android SDK direktori proyek anda aplikasi/libs.

– Sekarang Tambahkan mengkompilasi file('libs/org.apache.http.legacy.jar') dalam dependensi{} bagian dari membangun.gradle.

Komentar (1)

ApacheHttp Klien dihapus di v23 sdk. Anda dapat menggunakan HttpURLConnection atau pihak ketiga Http Client seperti OkHttp.

ref : https://developer.android.com/preview/behavior-changes.html#behavior-apache-http-client

Komentar (1)

Jika anda membutuhkan sdk 23, menambahkan ini untuk anda gradle:

android {
    useLibrary 'org.apache.http.legacy'
}
Komentar (0)

Hanya gunakan ini :-

android {
         .
         .
         .
 useLibrary 'org.apache.http.legacy'
         .
         .
         .
          }
Komentar (0)

Anda harus menambahkan satu baris

useLibrary 'org.apache.http.legacy'

dalam membangun.gradle(Modul: aplikasi), misalnya

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "25.0.0"

    useLibrary 'org.apache.http.legacy'

    defaultConfig {
        applicationId "com.avenues.lib.testotpappnew"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    testCompile 'junit:junit:4.12'
}
Komentar (0)

HttpClient tidak didukung di sdk 23 dan 23+.

Jika anda perlu menggunakan ke sdk 23, tambahkan kode di bawah ini ke gradle:

android {
    useLibrary 'org.apache.http.legacy'
}

Bekerja untuk saya. Semoga berguna untuk anda.

Komentar (1)

API yang target apakah anda dalam proyek anda?AndroidHttpClienthanya untuk API Level 8 <. dan silakan lihat di sini

nikmati kode anda:)

Komentar (0)

Seperti yang disebutkan sebelumnya, org.apache.http.klien.HttpClient tidak didukung lagi di:

SDK (API level) #23.

Anda harus menggunakan jawa.net.HttpURLConnection.

Jika anda ingin membuat kode anda (dan hidup) lebih mudah bila menggunakan HttpURLConnection, di sini adalah Wrapper dari kelas ini yang akan membiarkan anda melakukan operasi sederhana dengan MENDAPATKAN, POST dan MENEMPATKAN menggunakan JSON, seperti misalnya, melakukan HTTP MENEMPATKAN.

HttpRequest request = new HttpRequest(API_URL + PATH).addHeader("Content-Type", "application/json");
int httpCode = request.put(new JSONObject().toString());
if (HttpURLConnection.HTTP_OK == httpCode) {
    response = request.getJSONObjectResponse();
} else {
  // log error
}
httpRequest.close()

Merasa bebas untuk menggunakannya.

package com.calculistik.repository;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * <p>
 * Copyright © 2017, Calculistik . All rights reserved.
 * <p>
 * Oracle and Java are registered trademarks of Oracle and/or its
 * affiliates. Other names may be trademarks of their respective owners.
 * <p>
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common
 * Development and Distribution License("CDDL") (collectively, the
 * "License"). You may not use this file except in compliance with the
 * License. You can obtain a copy of the License at
 * https://netbeans.org/cddl-gplv2.html or
 * nbbuild/licenses/CDDL-GPL-2-CP. See the License for the specific
 * language governing permissions and limitations under the License.
 * When distributing the software, include this License Header
 * Notice in each file and include the License file at
 * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this particular file
 * as subject to the "Classpath" exception as provided by Oracle in the
 * GPL Version 2 section of the License file that accompanied this code. If
 * applicable, add the following below the License Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 * <p>
 * Contributor(s):
 * Created by alejandro tkachuk @aletkachuk
 * www.calculistik.com
 */
public class HttpRequest {

    public static enum Method {
        POST, PUT, DELETE, GET;
    }

    private URL url;
    private HttpURLConnection connection;
    private OutputStream outputStream;
    private HashMap params = new HashMap();

    public HttpRequest(String url) throws IOException {
        this.url = new URL(url);
        connection = (HttpURLConnection) this.url.openConnection();
    }

    public int get() throws IOException {
        return this.send();
    }

    public int post(String data) throws IOException {
        connection.setDoInput(true);
        connection.setRequestMethod(Method.POST.toString());
        connection.setDoOutput(true);
        outputStream = connection.getOutputStream();
        this.sendData(data);
        return this.send();
    }

    public int post() throws IOException {
        connection.setDoInput(true);
        connection.setRequestMethod(Method.POST.toString());
        connection.setDoOutput(true);
        outputStream = connection.getOutputStream();
        return this.send();
    }

    public int put(String data) throws IOException {
        connection.setDoInput(true);
        connection.setRequestMethod(Method.PUT.toString());
        connection.setDoOutput(true);
        outputStream = connection.getOutputStream();
        this.sendData(data);
        return this.send();
    }

    public int put() throws IOException {
        connection.setDoInput(true);
        connection.setRequestMethod(Method.PUT.toString());
        connection.setDoOutput(true);
        outputStream = connection.getOutputStream();
        return this.send();
    }

    public HttpRequest addHeader(String key, String value) {
        connection.setRequestProperty(key, value);
        return this;
    }

    public HttpRequest addParameter(String key, String value) {
        this.params.put(key, value);
        return this;
    }

    public JSONObject getJSONObjectResponse() throws JSONException, IOException {
        return new JSONObject(getStringResponse());
    }

    public JSONArray getJSONArrayResponse() throws JSONException, IOException {
        return new JSONArray(getStringResponse());
    }

    public String getStringResponse() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        StringBuilder response = new StringBuilder();
        for (String line; (line = br.readLine()) != null; ) response.append(line + "\n");
        return response.toString();
    }

    public byte[] getBytesResponse() throws IOException {
        byte[] buffer = new byte[8192];
        InputStream is = connection.getInputStream();
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        for (int bytesRead; (bytesRead = is.read(buffer)) >= 0; )
            output.write(buffer, 0, bytesRead);
        return output.toByteArray();
    }

    public void close() {
        if (null != connection)
            connection.disconnect();
    }

    private int send() throws IOException {
        int httpStatusCode = HttpURLConnection.HTTP_BAD_REQUEST;

        if (!this.params.isEmpty()) {
            this.sendData();
        }
        httpStatusCode = connection.getResponseCode();

        return httpStatusCode;
    }

    private void sendData() throws IOException {
        StringBuilder result = new StringBuilder();
        for (Map.Entry entry : params.entrySet()) {
            result.append((result.length() > 0 ? "&" : "") + entry.getKey() + "=" + entry.getValue());//appends: key=value (for first param) OR &key=value(second and more)
        }
        sendData(result.toString());
    }

    private HttpRequest sendData(String query) throws IOException {
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
        writer.write(query);
        writer.close();
        return this;
    }

}
Komentar (0)

Tambahkan dua baris di bawah dependensi

compile 'org.apache.httpcomponents:httpcore:4.4.1'
compile 'org.apache.httpcomponents:httpclient:4.5'

kemudian

useLibrary 'org.apache.http.legacy'

di bawah android

Komentar (0)

cara Lain adalah, jika anda memiliki httpclient.jar file yang kemudian dapat anda lakukan ini :

Paste anda .file jar di "libs" dalam proyek anda. Kemudian di gradle tambahkan baris ini di build.gradle(Modul:aplikasi)

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.0.0'
compile files('libs/httpcore-4.3.3.jar')
}
Komentar (0)