0

I am new to Android programming and am having some difficulty implementing a basic version of a message sender using the Loopj library.

Here is my code:

package com.test.app;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.loopj.android.http.*;

import org.apache.http.Header;

public class Test extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        AsyncHttpClient client = new AsyncHttpClient();
        client.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
            }
        });

    }

}

This code gives me two errors I am trying to work through:

  1. The line AsyncHttpClient client = new AsyncHttpClient(); underlines AsyncHttpClient and reports and error: "Class must either be declared abstract or implement abstract method for onFailure"

  2. The @Override designation for onSuccess and onFailure is underlined and the error states "The method does not override the method from its superclass".

How can I make this work in my Android Activity?

2
  • why you use this old http client without community and updates? Commented Jan 19, 2017 at 16:00
  • Well because Im new, and I see example code that pushes messages to Azure using this code. Do you have any suggestions? Im open to different approaches. Commented Jan 19, 2017 at 16:12

1 Answer 1

1

This is because of wrong imports.

You should replace:

import org.apache.http.Header;

with

import cz.msebera.android.httpclient.Header;

UPDATE:

BTW if you want to make HTTP POST you should use .post() instead of .get()

Sign up to request clarification or add additional context in comments.

3 Comments

This works, please suggest alternative libraries if you know of any.
If you want just client okhttp is the best library for that now. If you want REST API client retrofit2 with okhttp and rxjava is most modern stack. IMHO
Thanks for your help, Im going to try to adapt the old code to okhttp.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.