3

I have been struggling with this code for some time now. Why am I getting the follow error? method does not override method from its superclass Here is the code:

public void CanSendPassword() {
    asyncHttpClientPassword = new AsyncHttpClient();
    requestParamsPassword = new RequestParams();

    requestParamsPassword.put("email", mEmail);
    asyncHttpClientPassword.post(BASE_URL, requestParamsPassword, new JsonHttpResponseHandler()
    {
        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
            super.onSuccess(statusCode, headers, response);
            jsonResponse = response.toString();
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
            super.onFailure(statusCode, headers, throwable, errorResponse);
            jsonResponse = "failed";
        }
    }
    );
}

@override both are showing the same error and onSuccess and onFailure are greyed out too?

timv
  • 3,346
  • 4
  • 34
  • 43
  • Interestingly the code works??? Both onSuccess and onFailure are called when appropriate, but I still get the red error lines under the code. The error for onSuccess and onFailure are " Method does not override method from its superclass. The error for (statuscode....) are "cannot resolve method onsuccess...". I will keep digging as to how to make the errors disapear. – timv Mar 07 '17 at 22:20
  • I have this problem too right now. have you able to solve this? – Binsoi Mar 09 '17 at 02:54
  • still working on it so check back and I will get the answer some how – timv Mar 09 '17 at 04:03
  • 1
    Hi i was able to override the methods, it was about the parameters in the superclass did not match to the override method i set. i match the parameters on what is specified in this article https://loopj.com/android-async-http/doc/com/loopj/android/http/JsonHttpResponseHandler.html#onSuccess-int-cz.msebera.android.httpclient.Header:A-byte:A- – Binsoi Mar 09 '17 at 04:08
  • Any chance of posting some code as I tried your suggestion but must be doing something wrong as its still showing up as an error for me. Still digging. – timv Mar 09 '17 at 04:27
  • I posted my code, tell me if it doesn't work. – Binsoi Mar 09 '17 at 05:35

3 Answers3

3

Here is my code

TwitterRestClient

import android.content.Context;
import com.loopj.android.http.*;
import cz.msebera.android.httpclient.entity.StringEntity;

public class TwitterRestClient {
    private static final String BASE_URL = "https://www.example.com/api/";

    private static AsyncHttpClient client = new AsyncHttpClient();

    public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
    client.get(getAbsoluteUrl(url), params, responseHandler);
    }

    public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
    client.post(getAbsoluteUrl(url), params, responseHandler);
    }

    public static void post(Context ctx, String url, StringEntity entity, java.lang.String contentType, AsyncHttpResponseHandler responseHandler ){
    client.post(ctx,getAbsoluteUrl(url),entity,contentType,responseHandler);
    }

    private static String getAbsoluteUrl(String relativeUrl) {
        return BASE_URL + relativeUrl;
    }
}

This the method in my LoginAcitivity

public void testPost(StringEntity entity) throws JSONException {
    TwitterRestClient.post(getApplicationContext(),"api-auth/", entity,"application/json", new JsonHttpResponseHandler() {

        @Override
        public void onSuccess(int statusCode, cz.msebera.android.httpclient.Header[] headers, org.json.JSONArray response) {
            // If the response is JSONObject instead of expected JSONArray
            GlobalFunctions.ShowToast(getApplicationContext(),"test");
        }

        @Override
        public void onFailure(int statusCode, cz.msebera.android.httpclient.Header[] headers, java.lang.Throwable throwable, org.json.JSONArray errorResponse){
            GlobalFunctions.ShowToast(getApplicationContext(),"test1123");
        }

        @Override
        public void onFailure(int statusCode, cz.msebera.android.httpclient.Header[] headers, java.lang.Throwable throwable, org.json.JSONObject errorResponse){
            GlobalFunctions.ShowToast(getApplicationContext(),errorResponse.toString());
        }

        @Override
        public void onFailure(int statusCode, cz.msebera.android.httpclient.Header[] headers, java.lang.String responseString, java.lang.Throwable throwable){
            GlobalFunctions.ShowToast(getApplicationContext(),responseString);
        }
    });
}

This is what i call when user clicks the button

public void signIn(View v){
    try {

        String url = "/api-auth";

        JSONObject jsonParams = new JSONObject();

        jsonParams.put("username", "binsoi@gmail.com");
        jsonParams.put("password", "cornedbeef");

        StringEntity entity = new StringEntity(jsonParams.toString());
        client.post(context, url, entity, "application/json",
                responseHandler);

        testPost(entity);


    }  catch (Exception err)
    {
        GlobalFunctions.ShowToast(this, err.toString());
    }
}

Hope this will help you, tell me if it this doesn't work because i edited this before posting.

Binsoi
  • 383
  • 5
  • 13
0

Here is a solution that I have working but it does not solve my exact issue. I post this as I can not understand why this code works but my code does not.

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

EditText etSearchTerms;
Button btnSearch;
TextView tvSearchResults;
MyLoopjTask myLoopjTask;

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

    etSearchTerms = (EditText) findViewById(R.id.etSearchTerms);
    btnSearch = (Button) findViewById(R.id.btnSearch);
    tvSearchResults = (TextView) findViewById(R.id.tvSearchResults);

    btnSearch.setOnClickListener(this);

    myLoopjTask = new MyLoopjTask();
}

@Override
public void onClick(View v) {
    String searchTerm = etSearchTerms.getText().toString();
    etSearchTerms.setText("");
    // make loopj http call
    myLoopjTask.executeLoopjCall(searchTerm);
}
}

And here is the other class:

public class MyLoopjTask {


AsyncHttpClient asyncHttpClient;
RequestParams requestParams;

String BASE_URL = "https://team.mycompany.com/teambeta/LoginApp/Recovery/";
String jsonResponse;

public MyLoopjTask() {
    asyncHttpClient = new AsyncHttpClient();
    requestParams = new RequestParams();
}

public void executeLoopjCall(final String queryTerm) {

    requestParams.put("email", queryTerm);
    asyncHttpClient.post(BASE_URL, requestParams, new JsonHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
            super.onSuccess(statusCode, headers, response);
            jsonResponse = response.toString();
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
            super.onFailure(statusCode, headers, throwable, errorResponse);
            jsonResponse = "failed";
        }
    });
}
}

So why does this code work but not in my oringinal question?

timv
  • 3,346
  • 4
  • 34
  • 43
0

Finally solved my issue. It was not the code as I was doing everything correctly. I just had the old jar file still in the app/libs folder.

And here is the error I was seeing. Everything was working but Override and the super.onSuccess were red underlined.

enter image description here

enter image description here

Remove the android-async-1.4.3.jar file and the red lines disappeared.

timv
  • 3,346
  • 4
  • 34
  • 43