【问题标题】:can't get respons as String无法以字符串形式获得响应
【发布时间】:2020-07-19 07:49:41
【问题描述】:

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

I/错误: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);
            }
        });

如何获得字符串响应?

【问题讨论】:

    标签: java android web-services


    【解决方案1】:

    响应是一个字符串,但是你使用了错误的 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);
                    }
                });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-28
      • 1970-01-01
      相关资源
      最近更新 更多