6

I'm trying to create asynchronous rest call in Android using a library com.loopj.android.http.AsyncHttpClient however, i'm unable to implement AsyncHttpResponseHandler's overridden methods because Android Studio cannot find an appropriate import for Header class

How can I resolve the issue with the Header class not recognized by the Android Studio IDE?

 public void onSuccess(int statusCode, Header[] headers, byte[] response) {
                // called when response HTTP status is "200 OK"
            }

I see that if I mouse over and click on the Header, I get the following message, but I don't know how to select one of the multiple choices in this menu (moving the mouse dismisses it)

enter image description here

Alex Stone
  • 46,408
  • 55
  • 231
  • 407

3 Answers3

15

The answer by Alex is no longer valid as org.Apache.http is deprecated from API level 22.

Please replace all references of org.apache.http to cz.msebera.android.httpclient and use version 1.4.9 of loopj which includes the library.

For those who use gradle change your dependencies in build.gradle

dependencies {
  compile 'com.loopj.android:android-async-http:1.4.9'
}

Rebuild and then import cz.msebera.android.httpclient to use Header.

Sharjeel Ahmed
  • 2,051
  • 2
  • 17
  • 25
  • How do you use the cz.msemera.android.httpclient.header ? – ShahNewazKhan Sep 22 '15 at 22:10
  • If you are using Android studio, add the above command to build.gradle and recompile, then it will pull all the required libraries, then u can include it. – Sharjeel Ahmed Sep 23 '15 at 08:11
  • Thanks! Sorry for the ambiguity in my question, I meant how do you prepare the Header as I was unable to find the documentation for cz.msemera.anroid.httpclient.header – ShahNewazKhan Sep 23 '15 at 17:10
  • can you clarify what do you mean by "prepare"? – Sharjeel Ahmed Sep 25 '15 at 19:15
  • By prepare I meant how do you add headers to the header object. I ended up adding the header to the AsyncClient it self using the .addHeader() method. – ShahNewazKhan Sep 27 '15 at 15:31
  • 1
    @monotheist I've added this dependency **compile 'com.loopj.android:android-async-http:1.4.9'** to gradle. When I'm trying to Rebuild the project, I'm getting the error **Could not resolve com.loopj.android:android-async-http:1.4.9** – Srikar Reddy Oct 01 '15 at 12:31
  • I use jcenter() as my repository, what about yours? – Sharjeel Ahmed Oct 01 '15 at 17:50
  • 1
    This answer worked for me. It took a couple of tries for Android Studio to see the header from 'import cz.msebera.android.httpclient'. – Sparky1 Nov 19 '15 at 19:31
5

By the time I typed this question, I found the answer, hopefully it will help you:

Press Option + enter on Mac when this popup is visible, then select import org.apache.http.Header; from the dropdown menu:

enter image description here

Alex Stone
  • 46,408
  • 55
  • 231
  • 407
0

For those who use gradle change your dependencies in build.gradle

dependencies { compile 'com.loopj.android:android-async-http:1.4.9' compile 'cz.msebera.android:httpclient:4.4.1.2' }

In Ubuntu use ALT+Enter on the Header[] as explained above for the Mac

Which also changed the import I guessed at android.* to httpclient.header

import cz.msebera.android.*;                 
import cz.msebera.android.httpclient.Header;