Java android AsyncHttpClient如何设置标头'接受'一个'应用程序/xml'或'应用程序/json' [英] Java android AsyncHttpClient how set header 'Accept' an 'application / xml' or 'application / json'
本文介绍了Java android AsyncHttpClient如何设置标头'接受'一个'应用程序/xml'或'应用程序/json'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不知道如何为'applicatjon/json设置标头"Accept",现在我从服务器返回的响应是xml,但我想要一个json.设置标头时,服务器应向我发送xml.这是我的代码:
I don't know how I can set a header "Accept" for 'applicatjon/json now my response from server is a xml but I want to have a json. A server should send me a xml when I set a header. This is my code :
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库,这也是另一种方法.
Using AsyncHttpClient Library This is also another approach.
这篇关于Java android AsyncHttpClient如何设置标头'接受'一个'应用程序/xml'或'应用程序/json'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文