服务器响应失败

时间:2019-05-12 10:34:14

标签: android android-asynctask android-networking

我有一个快速服务器,用于向我的移动应用程序与服务器之间发送数据。 这是我的快递服务器从我的发布请求中发回的信息,我将我的应用程序发送到服务器上

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

发布请求被服务器发送并接收,我希望服务器将Ok响应发送回应用程序。这样做是正确的,因为我可以在控制台中正常发送。在移动应用程序中,我使用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);

        }




    });

当我收到确认信息时,即使状态码= 200,它也会自动转到onFailure。 android给出了一个半错误消息:

  

W / JsonHttpRH:onFailure(int,Header [],String,Throwable)没有被重写,但是收到了回调       org.json.JSONException:无法将响应解析为JSON数据

我如何能够解决此问题并使之成功?

1 个答案:

答案 0 :(得分:1)

将JsonHttpResponseHandler更改为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);

    }
});