0

I'm trying to call a custom API from my android app which will return a JSON object (and as far as I know Retrofit will map that into a desirable data type i.e. String for me in this case, please correct me if I'm wrong). So the problem is that when I try to make the API call, the server doesn't receive anything at all. Not sure how to isolate the error. I have no experience with Retrofit before this.

Also, I'm told to make a post request via body. Not sure what that implies in defining my interface. Any suggestions on that will be much appreciated.

Here's the code for the main activity:

 RestAdapter adap = new RestAdapter.Builder().setEndpoint(Endpoint).build();
 OnLoginAPI loginAPICalls = adap.create(OnLoginAPI.class);

    loginAPICalls.userloginCredentials(userCredentails.get(0), userCredentails.get(1), new Callback<String>() {
        @Override
        public void success(String APIresponse, Response response) {
            status = APIresponse;
        }

        @Override
        public void failure(RetrofitError error) {
            status = "API Error";
        }
    });
    return status;

And here's my interface :

public interface OnLoginAPI {

@POST("/v1/login")
public void userloginCredentials(
        @Field("username") String username,
        @Field("password") String password,
        Callback<String> response);

}

Here's the logcat log:

09-20 12:41:27.165      721-802/? W/AlarmManager﹕ FACTORY_ON= 0
09-20 12:41:27.165      721-802/? D/LightsService﹕ [SvcLED] SvcLEDTask
09-20 12:41:27.175      721-803/? D/BatteryService﹕ turn on LED for charging
09-20 12:41:27.185      721-738/? D/SensorManager﹕ registerListener :: handle = 7  name= GP2A Light Sensor delay= 200000 Listener= com.android.server.LightsService$4@4259fcc8
09-20 12:41:27.185      721-738/? D/LightsService﹕ [SvcLED] SvcLEDReceiver::isHappyNight = false
09-20 12:41:27.385      721-802/? D/SensorManager﹕ unregisterListener::  Listener= com.android.server.LightsService$4@4259fcc8
09-20 12:41:27.385      721-802/? D/LightsService﹕ [SvcLED] onSensorChanged::light value = 652.0
09-20 12:41:27.395      721-802/? I/LightsService﹕ fileWriteInt : /sys/class/sec/led/led_lowpower  value : 0
09-20 12:41:27.395      721-802/? D/LightsService﹕ setLightLocked is called
09-20 12:41:28.897      183-183/? E/SMD﹕ DCD ON
09-20 12:41:31.900      183-183/? E/SMD﹕ DCD ON
Gergely Kőrössy
  • 5,620
  • 3
  • 28
  • 44
Divesh
  • 1
  • 1
  • 5
  • check your logcat and post a log here. Which version of retrofit you are using? – Gaskoin Sep 20 '15 at 20:35
  • I've imported this dependency : "com.squareup.retrofit:retrofit:1.9.0". Also, posted the logcat log in the edited question. @Gaskoin – Divesh Sep 20 '15 at 21:10
  • @Divesh your logcat log does not have retrofit logs. Try the following code snippet to have the log level set: // production log level RestAdapter.LogLevel logLevel = RestAdapter.LogLevel.NONE; if (BuildConfig.DEBUG) { logLevel = RestAdapter.LogLevel.FULL; } In your rest adapter builder add the following: .setLogLevel(logLevel) – Ray Hunter Dec 06 '15 at 07:33

1 Answers1

0

Your retrofit interface declaration requires the @FormURLEncoded annotation so that retrofit knows how to process the body of the request.

sddamico
  • 2,130
  • 16
  • 13
  • Actually, it did have FormURLEncoded annotation right before the POST annotation earlier.. But itdidn't work so i was trying a few different things. But excluding it doesn't work either... – Divesh Sep 20 '15 at 21:29