Search code examples
androidjsonhttphttp-status-code-404loopj

LoopJ AsyncHttp returns 404


Hi I've been using this free GIT library for some time now and I just experience a very very weird problem

I have a URL (web API) that returns a JSON formatted data.

Yes, it exist on browser I tried all main browser out there and it is showing. The problem is when I used it on my AsyncHttp LoopJ it returns a 404 error, which fires

onFailure(int statusCode, Header[] headers, byte[] responseBytes, Throwable throwable)

status code is 404 and throwable message is "not found".

I've test other URL on my other project and it works only not this specific URL. That guarantees that it works on other URL.

I cant say that the API has a problem, since it shows JSON data on web browser. I clean the project rebuild nothing works.

Please anyone had the same struggle here. I check the manifest, check internet connection all good. Like I said it works on other URL, except this one.

PS. I like the simplicity of this library thats why I dont want to switch to other similar libraries out there.


Solution

  • What im using is this class

    public class TerminalClient {
    private static AsyncHttpClient client = new AsyncHttpClient();
    public static AsyncHttpClient syncHttpClient = new SyncHttpClient();
    
    public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        getClient().setTimeout(1000 * 10);
        getClient().get((url), params, responseHandler);
    }
    
    public static void asyncPost(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        getClient().setTimeout(1000 * 10);
        getClient().post((url), params, responseHandler);
    }
    
    public static void post(String url, RequestParams params, BaseJsonHttpResponseHandler baseJsonHttpResponseHandler) {
        getClient().setTimeout(1000 * 10);
        getClient().post((url), params, baseJsonHttpResponseHandler);
    }
    
    private static AsyncHttpClient getClient() {
        if (Looper.myLooper() == null)
            return syncHttpClient;
        return client;
    } 
    

    }

    I just switch to Post to Get. That did it, pretty weird. Yep.

    This can be a reference for future guys who will bump in this kind of problem.

    -Cheers