我试图向rest 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);
}
});
如何获得字符串响应?
响应是一个字符串,但您使用的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);
}
});