无法获得响应作为字符串

时间:2020-07-19 07:49:41

标签: java android web-services

我试图向其他Web服务服务器发出请求。 我的请求是JSON格式,我的响应是一个字符串。 当我尝试执行请求时,我得到:

I / ERROR:com.androidnetworking.error.ANError:org.json.JSONException: 类型为java.lang.String的值עסקה无法转换为JSONObject。

这是我的代码:

JSONObject res = new JSONObject();
try {//json format

    res.put("ErrorCode","000");
    Log.i("req",res.toString());
} catch (JSONException e) {
    e.printStackTrace();
}
AndroidNetworking.post("https://gateway20.pelecard.biz/services/GetErrorMessageHe")//send the json object to the method
        .addJSONObjectBody(res)
        .setTag("test")
        .setPriority(Priority.MEDIUM)
        .build()
        .getAsJSONObject(new JSONObjectRequestListener() {
            @Override
            public void onResponse(JSONObject response) {

                returnMyString(response.toString());

            }
            @Override
            public void onError(ANError error) {
                Log.i("ERROR",error.toString());
            }
            private void returnMyString(String myString) {
                Log.i("res",myString);
            }
        });

如何获取字符串响应?

1 个答案:

答案 0 :(得分:0)

响应是一个字符串,但是您使用了getAsString,这是错误的

  AndroidNetworking.post("https://gateway20.pelecard.biz/services/GetErrorMessageHe")//send the json object to the method
            .addJSONObjectBody(res)
            .setTag("test")
            .setPriority(Priority.HIGH)
            .build()
            .getAsString(new StringRequestListener() {
                @Override
                public void onResponse(String response) {
                    Log.e("TAG", "onResponse: "+response );
                }

                @Override
                public void onError(ANError anError) {
                    Log.e("TAG", "onError: "+anError.getMessage() );
                }
                private void returnMyString(String myString) {
                    Log.e("res",myString);
                }
            });