samedi 25 avril 2015

Android Rest Api Post 400 bad request


I'm working on an app that will send a password to the server to check if it's correct, the server will return 0,1,2,3 representing different statuses I'm not sure where this 400 bad request is coming from. I've called the server and received the json file but I'm suspecting it's how I parsed it? Any help is greatly appreciated.

AN UPDATE

an update. I realize part of the mistake here is that I'm trying to send a string without converting it to a JSON file first. That is why I've been receiving "status:0" which stands for an error occurred. I've converted my string into a RegisterRequest object which contains a string variable. Now I'm receiving status 2 which stands for incorrect password. However my bad request 400 still remains. PLEASE any help or suggestion will be very helpful to me

 @Headers("Content-Type: application/json")
 @POST("removed/{id}")
 RegisterResponse registerEvent(@Path("id") int ID, @Body RegisterRequest password)throws NetworkException;

RegisterResponse class:

public class RegisterResponse {
public String               status;
}

RegisterRequest class

public class RegisterRequest {

    String passcode ;

    public RegisterRequest(String passcode)
    {
        this.passcode=passcode;
    }
}

call to server:

try {
       RegisterResponse response = NetworkHelper.makeRequestAdapter(this)
               .create(testApi.class).register(ID, pass);

        if(response.status=="1"||response.status=="3")
        {
            return true;
        }
        else
            return false;
    } catch (NetworkException e) {
        e.printStackTrace();
    }
   return false;
}

Make Request Adapter

private static RestAdapter.Builder makeRequestAdapterBuilder(final Context context, ErrorHandler errorHandler, String dateTimeFormat)
    {
        RequestInterceptor requestInterceptor = new RequestInterceptor()
        {
            @Override
            public void intercept(RequestInterceptor.RequestFacade request)
            {
                request.addHeader("Authorization", AppPrefs.getInstance(context).getSessionToken());
            }
        };

        Gson gson = new GsonBuilder().setDateFormat(dateTimeFormat).create();
        GsonConverter gsonConverter = new GsonConverter(gson);

        RestAdapter.Builder builder = new RestAdapter.Builder()
                .setRequestInterceptor(requestInterceptor).setConverter(gsonConverter)
                .setLogLevel(RestAdapter.LogLevel.FULL).setLog(new AndroidLog("Boom!"))
                .setEndpoint(BASE_URL);

        if(errorHandler != null)
        {
            builder.setErrorHandler(errorHandler);
        }

        if(instance == null)
        {
            instance = new OkClient(makeTimeoutClient(READ_TIMEOUT, CONNECT_TIMEOUT));
        }
        builder.setClient(instance);

        return builder;
    }

Error code:

-13257/AndroidCall D/Boom!﹕ ---> HTTP POST https://serverurl/29522
04-24 23:18:04.957  13257-13257/AndroidCall  D/Boom!﹕ Authorization: Token 127e7r122y9dyq89ye2
04-24 23:18:04.957  13257-13257/AndroidCall  D/Boom!﹕ Content-Type: application/json
04-24 23:18:04.957  13257-13257/AndroidCall  D/Boom!﹕ Content-Length: 6
04-24 23:18:04.957  13257-13257/AndroidCall  D/Boom!﹕ "1444"
04-24 23:18:04.957  13257-13257/AndroidCall  D/Boom!﹕ ---> END HTTP (6-byte body)
04-24 23:18:05.058  13257-13257/AndroidCall  D/Boom!﹕ <--- HTTP 400 https://serverurl/29522 (97ms)
04-24 23:18:05.058  13257-13257/AndroidCall  D/Boom!﹕ Allow: POST, OPTIONS
04-24 23:18:05.058  13257-13257/AndroidCall  D/Boom!﹕ Content-Type: application/json
04-24 23:18:05.058  13257-13257/AndroidCall  D/Boom!﹕ Date: Sat, 25 Apr 2015 03:17:52 GMT
04-24 23:18:05.058  13257-13257/AndroidCall  D/Boom!﹕ Server: Apache/2.4.7 (Ubuntu)
04-24 23:18:05.058  13257-13257/AndroidCall  D/Boom!﹕ Vary: Accept,Cookie
04-24 23:18:05.058  13257-13257/AndroidCall  D/Boom!﹕ X-Frame-Options: SAMEORIGIN
04-24 23:18:05.058  13257-13257/AndroidCall  D/Boom!﹕ transfer-encoding: chunked
04-24 23:18:05.058  13257-13257/AndroidCall  D/Boom!﹕ Connection: keep-alive
04-24 23:18:05.058  13257-13257/AndroidCall  D/Boom!﹕ OkHttp-Selected-Protocol: http/1.1
04-24 23:18:05.058  13257-13257/AndroidCall  D/Boom!﹕ OkHttp-Sent-Millis: 1429931884981
04-24 23:18:05.058  13257-13257/AndroidCall  D/Boom!﹕ OkHttp-Received-Millis: 1429931885065
04-24 23:18:05.058  13257-13257/AndroidCall  D/Boom!﹕ {"status": "2"}
04-24 23:18:05.058  13257-13257/AndroidCall  D/Boom!﹕ <--- END HTTP (15-byte body)
04-24 23:18:05.058  13257-13257/AndroidCall  D/AndroidRuntime﹕ Shutting down VM
04-24 23:18:05.058  13257-13257/AndroidCall  W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x415c7ba8)
04-24 23:18:05.278  13257-13280/AndroidCall  D/dalvikvm﹕ GC_FOR_ALLOC freed 429K, 5% free 10221K/10684K, paused 21ms, total 21ms
04-24 23:18:05.358  13257-13280/AndroidCall  D/dalvikvm﹕ GC_FOR_ALLOC freed 479K, 5% free 10250K/10764K, paused 21ms, total 21ms
04-24 23:18:05.378  13257-13257/AndroidCall  E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: AndroidCall , PID: 13257
    retrofit.RetrofitError: 400 BAD REQUEST
            at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:388)
            at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
            at $Proxy2.register(Native Method)
            at AndroidCall .app.ui.activities.ListActivity.matchPasscode(ListActivity.java:386)
            at AndroidCall .app.ui.activities.ListActivity.access$200(ListActivity.java:48)
            at AndroidCall .app.ui.activities.ListActivity$6.onClick(ListActivity.java:301)
            at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)


Aucun commentaire:

Enregistrer un commentaire