最近在用Android-async-http来请求百度云服务提供TableStorage服务时,要用到get方法,而且还要携带body参数,乱码。
final String jsonObject_get = "{\n" +
" \"rowkey\": \"A\",\n" +
" \"cells\": [\n" +
" {\n" +
" \"column\": \"LL\"\n" +
" },\n" +
" {\n" +
" \"column\": \"HH\"\n" +
" }\n" +
" ]\n" +
"}";
void load_data(){
StringEntity entity = new StringEntity(jsonObject_get , "UTF-8");
client.addHeader("authorization",getauthorization.get(Headers.AUTHORIZATION));
client.addHeader("host","bts.gz.baidubce.com");
client.addHeader("x-bce-date",getauthorization.get("x-bce-date"));
client.get(context, url, entity, "application/json", new JsonHttpResponseHandler(){
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
super.onSuccess(statusCode, headers, response);
System.out.println(response.toString());
}
} );
}
遇到了以下的问题
I/System.out: {“result”:[{“rowkey”:“A”,“cells”:[{“column”:“HH”,“value”:"%E5%93%88%E5%93%88%E5%93%88%E5%93%88%E5%93%88%EF%BC%9A030214",“timestamp”:1588938991890},{“column”:“LL”,“value”:“lili%EF%BC%9A%E4%BD%A0%E5%A5%BD%E5%90%97”,“timestamp”:1588938991890}]}]}
返回的json中有"%E5%93%88%E5%93%88%E5%93%88%E5%93%88%E5%93%88%EF%BC%9A030214" 这类的编码。一开始以为是请求编码的问题,但把能设置的都设置了之后,问题依然存在,最后是在代码中添加了这句
String mytext2 = java.net.URLDecoder.decode(response.toString(),"utf-8");
就好了, 最终是这样
void load_data(){
StringEntity entity = new StringEntity(jsonObject_load, "UTF-8");
client.addHeader("authorization",getauthorization.get(Headers.AUTHORIZATION));
client.addHeader("host","bts.gz.baidubce.com");
client.addHeader("x-bce-date",getauthorization.get("x-bce-date"));
client.get(context, url, entity, "application/json", new JsonHttpResponseHandler(){
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
super.onSuccess(statusCode, headers, response);
String mytext2 = java.net.URLDecoder.decode(response.toString(),"utf-8");
System.out.println( mytext2);
}
} );
}
I/System.out: {“result”:[{“rowkey”:“A”,“cells”:[{“column”:“HH”,“value”:“哈哈哈哈哈:030214”,“timestamp”:1588938991890},{“column”:“LL”,“value”:“lili:你好吗”,“timestamp”:1588938991890}]}]}
刚刚接触http不久,暂时还找不到别的方法,先记录下,期待日后可能还会有更好的解决方法。