Domanda

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?

È stato utile?

Soluzione

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.

Altri suggerimenti

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

                    }

}

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top