private void doVolley(){
RequestQueue mRequestQueue = Volley.newRequestQueue(getActivity());
StringRequest mStringRequest = new StringRequest(
"http://www.baidu.com",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
String s = response;
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDefaultTextEncodingName("utf-8");
mWebView.loadDataWithBaseURL(null,s,"text/html","utf-8",null);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mWebView.loadDataWithBaseURL(null,"失敗","text/html","utf-8",null);
}
}
){
//這是重寫的方法,源代碼默認是外國人用的ISO-8859-1,如今改爲utf-8格式
@Override protected Response<String> parseNetworkResponse( NetworkResponse response) { try {//jsonObject要和前面的類型一致,此處都是String String jsonObject = new String( new String(response.data, "UTF-8")); return Response.success(jsonObject, HttpHeaderParser.parseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (Exception je) { return Response.error(new ParseError(je)); } } }; mRequestQueue.add(mStringRequest);}