android - Volley 库的问题
问题描述
我尝试在我的应用程序中获得响应,当我尝试使用 volley 库调用 API 时,它给出com.android.volley.ServerError
和响应代码 400。这是我的代码
RequestQueue requestQueue = Volley.newRequestQueue(this);
try
{
//start bottom
String url="http://api.tektravels.com/BookingEngineService_Air/AirService.svc/rest/GetBookingDetails";
url = url.replaceAll(" ", "%20");
//String url="http://api.tektravels.com/BookingEngineService_Air/AirService.svc/rest/Search/";
JSONObject jsonObject = new JSONObject();
jsonObject.put("EndUserIp","216.10.251.69");
jsonObject.put("TokenId","0307b931-bd7d-4860-9c4d-4d65103ebddc");
jsonObject.put("PNR","ZERD8U");
jsonObject.put("BookingId","1401272");
Log.i("JsonObject",jsonObject.toString());
JsonObjectRequest jsonobjectreq = new JsonObjectRequest(Request.Method.POST, url,jsonObject,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response)
{
Log.i("response",response.toString());
progressDialog.dismiss();
}
}
它在邮递员中工作。我得到的错误
com.android.volley.ServerError
日志是
10-12 10:34:19.525 7149-7189/com.farehawker E/Volley: [387] BasicNetwork.performRequest: Unexpected response code 400 for http://api.tektravels.com/BookingEngineService_Air/AirService.svc/rest/GetBookingDetails
10-12 10:34:19.529 7149-7149/com.farehawker I/Error: com.android.volley.ServerError
我发送的json是
{"EndUserIp":"216.10.251.69","TokenId":"0307b931-bd7d-4860-9c4d-4d65103ebddc","PNR":"ZERD8U","BookingId":"1401272"}
解决方案
AFAIK 您正在向服务器发送JsonObjectRequest ,为此,您还需要通过您的 Volley network call传递Content-Type 。
在您的 Volley NetworkCall 中附加此标头。
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
推荐阅读
- javascript - 实现闭包和函数的自调用
- java - 在eclipse java中组织导入
- java - Java 泛型 - 需要在 Object mapper 中泛化 A.class
- angular - 使用 angular 和 flex 动态排列目录卡
- google-cloud-platform - 在 Google Cloud 中,如何为每个函数设置 PubSub 的发布权限?
- javascript - 基于背景的自适应文本颜色
- ios - 尝试为 cloudkit 共享创建缩略图,但
- android - 如何在 ARCore 中调整对象的大小?
- octopus-deploy - Octopus - SQL 部署 DACPAC 社区贡献步骤
- python - 每天和地点多久出现一次问题?