Search code examples
javaandroidhttpgetrequest

Ljoop http get - callbacks not executed


I want to use the ljoop library to make http requests. However, when I execute the get method or post, it just jumps over it, doesn't go into the callbacks.

The ip address delivers a json result in the browser successfully. So it could either use the 10.0.2.2 I am aware of that but it works fine with 192.... in the browswer as well. I have added the internet permission but it just desn't seem to send the get request. The nodejs server with express does not receive anything only when using chrome on the emulator.

import android.app.Activity;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestHandle;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.SocketTimeoutException;
import java.util.List;

import cz.msebera.android.httpclient.Header;
import cz.msebera.android.httpclient.conn.ConnectTimeoutException;
import cz.msebera.android.httpclient.entity.StringEntity;
import cz.msebera.android.httpclient.message.BasicHeader;
import cz.msebera.android.httpclient.protocol.HTTP;
import schaschinger.eggermode.Restservice.Models.Item;
import schaschinger.eggermode.Restservice.Models.Stammkunde;


public class HttpRequestTask {

    public static final String URL_STAMMKUNDEN = "http://192.168.1.190:9999/stammkunden/stammkunden";
    public static final String URL_ITEMS = "http://192.168.1.190:9999/item/items";

    Activity activity;
    final AsyncHttpClient asyncHttpClient;
    HttpResponseHandler httpResponseHandler;


    public HttpRequestTask(Activity activity) {
        this.activity = activity;
        this.asyncHttpClient = new AsyncHttpClient();
        httpResponseHandler = new HttpResponseHandler();

        this.asyncHttpClient.allowRetryExceptionClass(IOException.class);
        this.asyncHttpClient.allowRetryExceptionClass(SocketTimeoutException.class);
        this.asyncHttpClient.allowRetryExceptionClass(ConnectTimeoutException.class);
    }

    public boolean insertStammkunde(Stammkunde stammkunde) {

        String stammkundenJson = stammkunde.getJson();

        StringEntity stringEntity = null;
        try {
            stringEntity = new StringEntity(stammkundenJson);
        } catch (UnsupportedEncodingException e) {
            return false;
        }
        stringEntity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));

        RequestHandle requestHandle = asyncHttpClient.post(activity.getApplicationContext()
                , HttpRequestTask.URL_STAMMKUNDEN, stringEntity, "application/json", httpResponseHandler);

        return requestHandle.isFinished();
    }

    public List<Item> getItems(){

        asyncHttpClient.setConnectTimeout(1000);

        asyncHttpClient.get("https://www.google.com", new AsyncHttpResponseHandler() {

            @Override
            public void onStart() {
                // called before request is started
            }

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

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
                // called when response HTTP status is "4XX" (eg. 401, 403, 404)
            }

            @Override
            public void onRetry(int retryNo) {
                // called when request is retried
            }
        });

        return null;
    }


}

Solution

  • Your code isn't supposed to invoke the callback when control reaches the row with the asynchronous call. Instead, the callback is invoked later, when the server sends a response to your request. This is what "asynchronous" means.

    You can return the items wrapped in a Future by, for instance, changing the return type of getItems to CompletableFuture<List<Item>> and returning a CompletableFuture. Call that future's complete in the onSuccess callback and completeExceptionally in the onFailure callback.

    Then, you have a choice of either calling the future's get to synchronously get the result (which will block until the request completes or times out), or assign callbacks with handle (or thenAccept and exceptionally) from CompletionStage, which CompletableFuture implements.