0

I am trying to write an Android app that uses com.loopj.android.http.AsyncHttpClient. Although I wrote an Android app some years ago, I am very rusty, so please be gentle with me!

In my app, I do a get, and none of the callback virtual methods are called. To simplify, and make sure it wasn't something about my app, I made a new project based on the SupportAppNavigation project that comes with the SDK. I modified the onCreate code as follows:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Log.i("com.trumphurst", "Creating AsyncHttpClient");
    AsyncHttpClient client = new AsyncHttpClient();
    client.setTimeout(5000);
    Log.i("com.trumphurst", "Getting www.google.com");
    client.get("http://www.google.com", new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(String response) {
            Log.i("com.trumphurst", "Success");
        }
        @Override
        public void onFailure(Throwable error, String content) {
            Log.i("com.trumphurst", "Failure");
        }
    });
    setListAdapter(new SampleAdapter(querySampleActivities()));
}

(Note that this is the only code change I have made to the example application that comes with the SDK.)

I started the app under the debugger, and monitored the logcat output. I saw "Creating AsyncHttpClient" and "Getting www.google.com", then nothing.

I have added some permissions to the manifest - it now starts:

<manifest android:versionCode="1"
    android:versionName="1"
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.support.appnavigation">

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<application android:label="@string/app_name">
    <activity android:name=".app.AppNavHomeActivity"
            android:label="@string/app_nav_home_label">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Clearly I must be doing something wrong - can anyone put me right?

Nikki Locke
  • 2,759
  • 6
  • 29
  • 53
  • Did you add the requisite permissions in your manifest file ? I could not see anything wrong with the piece of code that you have shared .. http://loopj.com/android-async-http/ – The Dark Knight Jun 18 '13 at 11:03
  • I tried overriding onStart and onFinish, with logging - onStart is called, but onFinish is not. – Nikki Locke Jun 18 '13 at 11:04
  • What about the permissions in manifest ? Do you have them ? Also you might need to share a bit more of the code piece with us to make enough sense of it .. – The Dark Knight Jun 18 '13 at 11:04
  • Every android project has Manifest.xml file – Borys Jun 18 '13 at 11:06
  • That's probably the problem - what requisite permissions should I add? Should there be some output somewhere in the debugger that tells me if a needed permission is missing? – Nikki Locke Jun 18 '13 at 11:07
  • Look at @Borys answer. He has given the permissions that you need to add in your manifest . – The Dark Knight Jun 18 '13 at 11:08

3 Answers3

5

I think you forget add permission in your AndroidManifest file:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Read more about Manifest file here.

UPDATED: Here is an example of manifes file from loopj github

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.loopj.android.http"
    android:versionName="1.4.3"
    android:versionCode="1">
    <application
        android:name="android_async_http">
    </application>
    <uses-sdk android:minSdkVersion="3" />
    <uses-permission android:name="android.permission.INTERNET" />
</manifest> 

You can see INTERNET permission here.

Borys
  • 1,793
  • 2
  • 17
  • 32
  • Thankyou. I have added these (using the eclipse friendly editor), but it seems to make no difference. – Nikki Locke Jun 18 '13 at 11:15
  • I shut down everything (emulator, and eclipse), and restarted again, and it works this time. Thanks very much - I knew it was something stupid I hadn't done. – Nikki Locke Jun 18 '13 at 11:30
  • Do you have network ON? Do you see something in LogCut? You can use sources instead of Jar file and add more logs messages there to get what is wrong. here is the link to the opensource project: https://github.com/loopj/android-async-http – Borys Jun 18 '13 at 11:31
2

Change AsyncHttpResponseHandler() to TextHttpResponseHandler() then your Overrides should be:

myClient.get(url, new TextHttpResponseHandler() {

                    @Override
                    public void onFailure(int i, Header[] headers, String s, Throwable throwable) {

                     }

                    @Override
                    public void onSuccess(int ii, Header[] headers, String s) {

                    }

}
cobus swart
  • 176
  • 1
  • 5
0

Try to also add the onStart and onComplete methods to the callback.

The problem with the AsyncHttpClient is that it wont throw an error if your JSON returned is correctly encoded or not

e.g. I had the same problem as you but noticed that both the onSuccess and onComplete methods were being called but not success or failure. Found it to be the way my server was returning JSON. I was trying to return an associated string array that I created my self and missed the [] infont of the arrayname

Problem: $result = array('result'=>'OK'); Solution: $result[] = array('result'=>'OK');

Not saying yours would be the same syntax problem but it wouldn't hurt to look over what is being returned

julianpitt
  • 552
  • 6
  • 15