url = updateUrl;//升级文件路径
// StringRequest request = new StringRequest(url, new Response.Listener<String>() {
MyStringRequest request = new MyStringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String arg0) {
updatejson(arg0.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
// TODO Auto-generated method stub
}
});
new Volley().newRequestQueue(this).add(request);
2 乱码问题解决方案
解决这个问题有两个方法:
- 在服务器返回的Response中设定编码格式为UTF-8(但如果我们请求的是他人服务器上的内容,这就没有办法操作)
- 重新定义一个StringRequest类,在解析返回的Response时直接设定编码格式为UTF-8
public class MyStringRequest extends StringRequest {
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String parsed;
try {
parsed = new String(response.data, "utf-8"));
} catch (UnsupportedEncodingException e) {
parsed = new String(response.data);
}
return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
}
}
作者:chengshuyuan_uestc
来源:CSDN
原文:https://blog.csdn.net/chengshuyuan_uestc/article/details/51755191
版权声明:本文为博主原创文章,转载请附上博文链接!