我不知道如何为“applicatjon/json”设置标头“Accept”,现在我来自服务器的响应是 xml,但我想要一个 json。当我设置标头时,服务器应该向我发送一个 xml。这是我的代码:
final JSONObject requestObject = new JSONObject();
try {
requestObject.put("company", "TEST");
requestObject.put("user", "pawelo");
requestObject.put("secure_password", "8ce241e1ed84937ee48322b170b9b18c");
requestObject.put("secure_device_id", "C4CA4238A0B923820DCC509A6F75849B");
} catch (JSONException e) {
e.printStackTrace();
}
StringEntity entity = null;
try {
entity = new StringEntity(requestObject.toString());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
client.post(this, url, entity, "application/json",
new BaseJsonHttpResponseHandler("UTF-8") {
@Override
public void onSuccess(int statusCode, Header[] headers, String rawJsonResponse, Object response) {
Log.e("sdasa " , rawJsonResponse + " " + statusCode);
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, String rawJsonData, Object errorResponse) {
}
@Override
protected Object parseResponse(String rawJsonData, boolean isFailure) throws Throwable {
return null;
}
});
JSONObject json = new JSONObject();
JSONObject dataJson = new JSONObject();
dataJson.put("body", message);
dataJson.put("title", getFirebaseUser().getDisplayName());
json.put("notification", dataJson);
json.put("registration_ids",jsonArray);
StringEntity se = new StringEntity(json.toString(), "UTF-8");
AsyncHttpClient client = new AsyncHttpClient();
client.addHeader("Accept", "application/json");
client.addHeader("Content-type", "application/json;charset=utf-8");
client.addHeader("Authorization", "key=" + "xxxxxxxxxxxx");
client.post(getInstance(), "your url", se, "application/json;charset=utf-8", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
Log.e("success_noti", new String(responseBody) + "");
if(isEnd){
getMessage.getMessageFunc(END);
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
Log.e("fail_noti", new String(responseBody) + "");
}
});
使用AsyncHttpClient Library 这也是另一种方法。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)