Search code examples
androidandroid-asynctaskandroid-networking

Response failed with server


I hava a express server to send data to and from the server for my mobile app. this is what my express server sends back from my post request I send on my app to the server

then(() => { console.log("ok send"); res.status(200).send("ok")}).catch((err) => {console.log(err); res.status(400).send('error')})

The post request gets send and recieved by the server and I want te server to send an Ok response back to the app. It does this correct because I get an ok send in console. In the mobile app I use a AsyncHttpClient.

 AsyncHttpClient.post("/url/" + id,json,new JsonHttpResponseHandler(){
        @Override
        public void onSuccess(int statusCode, Header[] headers, String response) {
            super.onSuccess(statusCode, headers, response);

            System.out.println("succes");

        }

        @Override
        public void onFailure(int statuscode,Header[] headers, String response, Throwable trowable){
            super.onFailure(statuscode,headers,response,trowable);
            System.out.println("fail");
            System.out.println(response);
            System.out.println("statuscode = " + statuscode);

        }




    });

When I recieve the ok back it automatically goes to the onFailure even if statuscode = 200. And android gives a semi error message:

W/JsonHttpRH: onFailure(int, Header[], String, Throwable) was not overriden, but callback was received org.json.JSONException: Response cannot be parsed as JSON data

How would I be able to fix this problem and make it go to onsucces ? thx


Solution

  • change JsonHttpResponseHandler to TextHttpResponseHandler

    client.post("/url/" + id,json,new TextHttpResponseHandler(){
        @Override
        public void onSuccess(int statusCode, Header[] headers, String response) {
            System.out.println("succes");
    
        }
    
        @Override
        public void onFailure(int statuscode,Header[] headers, String response, Throwable trowable){
            System.out.println("fail");
            System.out.println(response);
            System.out.println("statuscode = " + statuscode);
    
        }
    });