can't use HttpPost in Android Studio after successful Gradle sync - android

I'm trying to use HttpPost as I did in the past in Android. I realize that it has been removed from recent Android versions. Following the solutions on a similar StackOverflow question, I added this to my Gradle dependencies:
compile 'org.apache.httpcomponents:httpcore:4.4.1'
compile 'org.apache.httpcomponents:httpclient:4.5'
After a Gradle sync that complained of no errors, I still cannot import the HttpPost program. In fact, the org.apache.http.client.methods package does not resolve at all.
I also tried what the Apache HttpClient website suggested, which was to use this instead in my Gradle dependencies:
compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
Once again, the sync went fine, and this time the package resolved, but not the HttpPost program.
I'd be grateful for any suggestions.

Just add this line in your build.gradle inside android plugin :
useLibrary 'org.apache.http.legacy'

The Apache HttpClient 4.3 for Android meant to be an update for the legacy 4.0 alpha version that was included in early Android versions. To avoid naming conflicts some on the updated classes got the postfix HC4, like HttpPostHC4 for example, which one had to use instead.
But now that the old HttpClient was removed form Android, the old classes like HttpPost do not exist anymore.
The official way to get the HttpClient library on newer Android version is to use:
android {
useLibrary 'org.apache.http.legacy'
}
as documented here.

Related

Error when publish to Google play and add to library to gradle

I want to publish my app to Google PLay
Error : "We found that the application uses the old version of the Google Play Developer API. From December 1, 2019, support for versions 1 and 2 of this API will cease. Update it to version 3 before this date. Read more ..."
I found, how to fix it, I need to gradle this library
implementation 'com.google.apis:google-api-services-androidpublisher:v3-rev95-1.25.0'
But when I build APK, studio show me error
<issue
id="DuplicatePlatformClasses"
severity="Fatal"
message="`httpclient` defines classes that conflict with classes now provided by Android. Solutions include finding newer versions or alternative libraries that don&apos;t have the same problem (for example, for `httpclient` use `HttpUrlConnection` or `okhttp` instead), or repackaging the library using something like `jarjar`."
category="Correctness"
priority="8"
summary="Duplicate Platform Classes"
explanation="There are a number of libraries that duplicate not just functionality of the Android platform but using the exact same class names as the ones provided in Android -- for example the apache http classes. This can lead to unexpected crashes.
To solve this, you need to either find a newer version of the library which no longer has this problem, or to repackage the library (and all of its dependencies) using something like the `jarjar` tool, or finally, rewriting the code to use different APIs (for example, for http code, consider using `HttpUrlConnection` or a library like `okhttp`).">
<location
file="D:\Applications\Radio\app\build.gradle"/>
</issue>
what am I doing wrong ?
Add this to project gradle
repositories {
mavenCentral()
}
Below code to your app gradle
dependencies {
compile 'com.google.apis:google-api-services-androidpublisher:v3-rev95-1.25.0'
}
Add this...
android {
...
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
}
}

How to import library URLEncodedUtils in android studio?

I want to import org.apache.http.client.utils.URLEncodedUtils in android studio. It have to add library in build gradle I add this code
implementation 'org.apache.httpcomponents:httpclient:4.5.6'
It show error like this.
httpclient defines classes that conflict with classes now provided by
Android. Solutions include finding newer versions or alternative
libraries that don't have the same problem (for example, for
httpclient use HttpUrlConnection or okhttp instead), or repackaging
the library using something like jarjar.
If I remove implementation in build gradle it show error can not resolv symbol URLEncodedUtils.
How to import library URLEncodedUtils android studio ?
Remove implementation 'org.apache.httpcomponents:httpclient:4.5.6'
and add this to your gradle:
android {
useLibrary 'org.apache.http.legacy'
}
You also may try to download and include HttpClient jar directly into your project or use OkHttp instead.
Hope this help!
Add next block to build.gradle located in app module
configurations {
all {
exclude module: 'httpclient'
}
}

What's the difference between principles of useLibrary and compile files('') in build.gradle?

After Android 6.0 releases, Support for the Apache HTTP client is removed. If our app is using this client and targets Android 2.3 (API level 9) or higher, HttpURLConnection class is recommended. It's said that this API is more efficient because it reduces network use through transparent compression and response caching, and minimizes power consumption. If we want to continue using the Apache HTTP APIs, you must first declare the following compile-time dependency in our build.gradle file:
android {
useLibrary 'org.apache.http.legacy'
}
The legacy jar is in Android SDK, whose path is sdk/platforms/android-23/optional/. So, it is nearly independent. Meanwhile, this apache legacy jar is putted into optional/ in Android SDK, so what is optional/? What does that mean?
Also we know, we can put this jar into libs and then declare it in our build.gradle file like this:
dependencies {
compile files('libs/org.apache.http.legacy.jar')
}
Both methods worked as expected when I tested.
But why?
What's the difference between useLibrary and compile files('') in build.gradle? Only because the legacy jar file is in android SDK so I can declare useLibrary in build.gradleto use it? Could I use other jars in this way?
Could somebody provide some ideas about this?
useLibrary adds the library to classpath while compiling but does not bundle the library with the application.
compile dependencies are in classpath at compile time and additionally they get shipped with your APK.
For the Apache HttpClient support, use useLibrary when compiling with SDK 23+. The library is already there in the target platform. It is just not there in the compile SDK.

Android Gradle Apache HttpClient does not exist?

I am trying to convert an IntelliJ project to the Gradle system of Android Studio but I am running into errors with Apache HttpClient? Am I missing something, the errors I am getting are as follows:
Error:(10, 30) error: package org.apache.http.client does not exist
Error:(11, 30) error: package org.apache.http.client does not exist
Error:(12, 37) error: package org.apache.http.client.entity does not exist
Error:(13, 38) error: package org.apache.http.client.methods does not exist
Error:(14, 38) error: package org.apache.http.client.methods does not exist
Error:(15, 38) error: package org.apache.http.client.methods does not exist
Error:(16, 35) error: package org.apache.http.impl.client does not exist
Error:(134, 33) error: cannot find symbol class HttpUriRequest
Error:(164, 39) error: cannot find symbol class HttpUriRequest
Error:(106, 17) error: cannot find symbol class HttpGet
Error:(106, 39) error: cannot find symbol class HttpGet
Error:(117, 17) error: cannot find symbol class HttpPost
Error:(117, 40) error: cannot find symbol class HttpPost
Error:(125, 43) error: cannot find symbol class UrlEncodedFormEntity
Error:(135, 9) error: cannot find symbol class HttpClient
Error:(135, 33) error: cannot find symbol class DefaultHttpClient
Error:(155, 18) error: cannot find symbol class ClientProtocolException
Error:(165, 9) error: cannot find symbol class HttpClient
Error:(165, 33) error: cannot find symbol class DefaultHttpClient
Error:(185, 18) error: cannot find symbol class ClientProtocolException
My build.gradle file has the following dependencies:
dependencies {
compile 'com.google.android.gms:play-services:+'
compile 'org.apache.httpcomponents:httpclient:4.2.6'
compile 'org.apache.httpcomponents:httpmime:4.2.6'
compile files('libs/core.jar')
}
It seems a lot of people are getting a similar problem but neither SO or Google have a solution so I am hoping this question will help future searchers.
if you are using target sdk as 23 add below code in your build.gradle
android{
compileSdkVersion 23
buildToolsVersion '23.0.1'
useLibrary 'org.apache.http.legacy'
}
and change your buildscript to
classpath 'com.android.tools.build:gradle:1.3.0'
for more info follow this link
I had this problem and then found these pages:
Here you can see that apache library is deprecated, but it's not removed, so it should work. It doesn't.
See.
And here you can see how to include apache library to your project
See.
I resolved problem by adding following to my build.gradle file as recommended in second link.
android {
useLibrary 'org.apache.http.legacy'
}
However this only works if you are using gradle 1.3.0-beta2 or greater, so you will have to add this to buildscript dependencies if you are on a lower version:
classpath 'com.android.tools.build:gradle:1.3.0-beta2'
Add this library into build.gradle
android {
useLibrary 'org.apache.http.legacy'
}
I suggest you replace the deprecated apache HttpClient with the new HttpURLConnection.
That's a cleaner solution, it's quite easy to migrate, and generally it's better to stick to the latest SDK changes than trying to hack/patch/workaround: you usually regret it later :)
Step 1
HttpGet httpGet = new HttpGet(url);
becomes:
URL urlObj = new URL(url);
Step 2
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpResponse response = httpClient.execute(httpGet, localContext);
InputStream is = response.getEntity().getContent();
becomes:
HttpURLConnection urlConnection = (HttpURLConnection) urlObj.openConnection();
InputStream is = urlConnection.getInputStream();
Step 2 bis
int status = response.getStatusLine().getStatusCode();
becomes:
int status = urlConnection.getResponseCode();
copy org.apache.http.legacy.jar which is in Android/Sdk/platforms/android-23/optional folder to to app/libs
and also added this line to app.gradle file
compile files('libs/org.apache.http.legacy.jar')
But if you're using more jar libraries, you can use this way
compile fileTree(dir: 'libs', include: ['*.jar'])
Basically all you need to do is add:
useLibrary 'org.apache.http.legacy'
To your build.gradle file.
I ran into similar problems, you might be able to get it to work using a similar method.
First, try this with your current configuration, exclude httpclient from httpmime:
dependencies {
compile 'com.google.android.gms:play-services:+'
compile ('org.apache.httpcomponents:httpmime:4.2.6'){
exclude module: 'httpclient'
}
compile 'org.apache.httpcomponents:httpclient:4.2.6'
}
In my case, I fixed it by using the following jars :
httpclient-android-4.3.5.1.jar
httpmime-4.3.5.jar
Then, in the build.gradle, excluding httpclient from httpmime:
dependencies {
compile 'com.google.android.gms:play-services:+'
compile('org.apache.httpcomponents:httpmime:4.3.5') {
exclude module: 'httpclient'
}
compile 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
}
I ran into the same issue. Daniel Nugent's answer helped a bit (after following his advice HttpResponse was found - but the HttpClient was still missing).
So here is what fixed it for me:
(if not already done, commend previous import-statements out)
visit http://hc.apache.org/downloads.cgi
get the 4.5.1.zip from the binary section
unzip it and paste httpcore-4.4.3 & httpclient-4.5.1.jar in project/libs folder
right-click the jar and choose Add as library.
Hope it helps.
Perfect Answer by Jinu and Daniel
Adding to this
I solved the Issue by Using This, if your compileSdkVersion is 19(IN MY CASE)
compile ('org.apache.httpcomponents:httpmime:4.3'){
exclude group: 'org.apache.httpcomponents', module: 'httpclient'
}
compile ('org.apache.httpcomponents:httpcore:4.4.1'){
exclude group: 'org.apache.httpcomponents', module: 'httpclient'
}
compile 'commons-io:commons-io:1.3.2'
else if your compileSdkVersion is 23
then use
android {
useLibrary 'org.apache.http.legacy'
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
}
}
This is what I did, and it works for me.
step 1: add this in the build.grade(module: app)
compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
step 2: sync the project and done.
After spending days on the subject, I managed to solve it finally. Unfortunately we need it if we want to use expansion files. The solution has been provided by a few people here, but for me the answers were missing a few minor detail that could have saved a lot of time for me. Here is the sequence of events for novices like me to save your precious time.
First I imported the "library" folder from D:\Users\Imre\AppData\Local\Android\sdk1\extras\google\play_licensing to the project by going into File, New, Import Module and navigating to the "library" folder. If you open the SDK Manager and click on "Launch Standalone SDK Manager" in the bottom of the popup screen, you can hover your pointer over the "Extras" folder down in the bottom and the small yellow info will tell you where you will find the package that you need to import.
Once that is done, go down to the "library" section in the left pane with "Project" and within it the "Android" tab open. Open the java section of the library and open the APKExpansionPolicy class.
If you have errors and the import org.apache.http.NameValuePair and the import org.apache.http.client.utils.URLEncodedUtils are pale grey, open the build.gradle(Project:whatever) and make sure that in "buildscript" under "dependancies" you have the "classpath 'com.android.tools.build:gradle:1.5.0" included. The 1.5.0 might be different in your case. I guess it depends on your Studio version. Mine is 1.5.1. If yours is newer, you will be reminded to update the numbers.
After that, go to "build.gradle(Module:library) and include the "useLibrary 'org.apache.http.legacy' into the "android"section. Sync Now (R/H top corner) should be offered. Do it.
If you get further error messages (I wouldn't know it because I did it the other way round), the Apache files might be missing from your project, as apache is not supported anymore. Find them in C/D:Users/Yourname/AppData/Local/Android/Sdk/platforms/android-23/optional and copy/paste them into yourproject/app/libs folder. You should have an optional.json and an org.apache.http.legacy.jar file in there. You might be better off going to apache's website and dowloading the newest version: http://hc.apache.org/downloads.cgi Unzip it anywhere you want, open it, go to "lib", copy the httpclient-4.5.1.jar file and replace the org.apache.http.legacy.jar file with it.
Sync and rebuild/clean your project and it should be good to go.
Just in case, open your terminal (L/H bottom of Android Studio) and type in "gradlew clean". It will install a few things. Once it is done, type in "gradlew assemble". It will take a few minutes to finish, but it will give you the errors if you have any of course.
If you cannot type in anything in the Terminal, launch command prompt (Windows button+R), type in cmd, hit OK, right click on the title of the small black popup screen (C:\WINDOWS\system32\cmd.exe), Properties, Options, in the bottom of the window check that "Use legacy console" is thicked. Restart A.Studio.
Good luck!
The version of the Apache HTTP client provided on stock Android was very very old.
Google Android 1.0 was released with a pre-BETA snapshot of Apache HttpClient. To coincide with the first Android release Apache HttpClient 4.0 APIs had to be frozen prematurely, while many of interfaces and internal structures were still not fully worked out. As Apache HttpClient 4.0 was maturing the project was expecting Google to incorporate the latest code improvements into their code tree. Unfortunately it did not happen.
While you could keep using the old deprecated library via the useLibrary 'org.apache.http.legacy' workaround (suggested by #Jinu and others), you really need to bite the bullet and update to something else, for example the native Android HttpUrlConnection, or if that doesn't meet your needs, you can use the OkHttp library, which is what HttpUrlConnection is internally based upon anyway.
OkHttp actually has a compatibility layer that uses the same API as the Apache client, though they don't implement all of the same features, so your mileage may vary.
While it is possible to import a newer version of the Apache client (as suggested by #MartinPfeffer), it's likely that most of the classes and methods you were using before have been deprecated, and there is a pretty big risk that updating will introduce bugs in your code (for example I found some connections that previously worked from behind a proxy no longer worked), so this isn't a great solution.
I had to post as none of the above answers worked completely for me.
I am using Android Studio
classpath 'com.android.tools.build:gradle:1.5.0'
compileSdkVersion 23
buildToolsVersion "23.0.3"
Step 1: Download lastest jar file (http://www-eu.apache.org/dist//httpcomponents/httpclient/binary/httpcomponents-client-4.5.2-bin.zip)
Step 2: Copy paste the .jar file to the libs folder (create if does not exist already) in your module (can be app or library)
Step 3: Right click on the jar and "Add as Library". It will automatically add the jar file as a dependency in your module's gradle file
Step 4: Now automatically your problem will get resolved but in case you are using proguard in your app, it will give you warning about duplicate class files and won't let you build. It is a known bug and you need to add following to your proguard-rules
-dontwarn org.apache.commons.**
-keep class org.apache.http.** { *; }
-dontwarn org.apache.http.**
Good Luck!
In my case, I updated one of my libraries in my android project.
I'm using Reservoir as my cache storage solution: https://github.com/anupcowkur/Reservoir
I went from:
compile 'com.anupcowkur:reservoir:2.1'
To:
compile 'com.anupcowkur:reservoir:3.1.0'
The library author must have removed the commons-io library from the repo so my app no longer worked.
I had to manually include the commons-io by adding this onto gradle:
compile 'commons-io:commons-io:2.5'
https://mvnrepository.com/artifact/commons-io/commons-io/2.5
I cloned the following: https://github.com/google/play-licensing
Then I imported that into my project.

Cannot resolves symbol packetInterceptor

I am trying to run the XMPP-based App Server based on Smack library
I'v added the required dependencies based on smack 4.1.0. I have problem with one Import:
import org.jivesoftware.smack.PacketInterceptor;
I experience the error message
Cannot resolves symbol PacketInterceptor
How do I resolve this?
If you are using gradle, use following dependencies
compile "org.igniterealtime.smack:smack-java7:4.0.1"
compile "org.igniterealtime.smack:smack-extensions:4.0.6"
compile "org.igniterealtime.smack:smack-tcp:4.0.6"
Or download the jar for the smack 4.0.6
Apparently, in the latest 4.1.0-Beta version they made some change to API but lack of enough clear documents.

Categories

Resources