当运行以下代码时,不会从服务器收到任何响应。在php方面,以下是标题行被添加。 header(" Content-Type:text / json; Charset = UTF-8");.在post方法成功运行之前,但现在使用此标头内容类型没有收到服务器的响应。此行已添加到请求.addHeader(" Content-Type"," text / json; Charset = UTF-8") 但仍然没有结果。标题分配正确吗?并且还可以okhttp调用接受特殊字符。非常欢迎帮助。
RequestBody body = new FormEncodingBuilder()
.add("visitor",MyApplication.pref.GetPreferences("acc_type").toLowerCase())
.add("id_customer",MyApplication.pref.GetPreferences("id_customer"))
.add("sector",sector)
.add("search_who_what",MyApplication.pref.GetPreferences("quiQuoi"))
.add("search_location",str_ou)
.add("agenda",MyApplication.pref.GetPreferences("agenda_envelop"))
.add("sort_by", "")
.add("advanced_agenda","")
.add("distance", "")
.build();
public String post(String url, RequestBody body) {
Request request = new Request.Builder()
.url(url)
.addHeader("Content-Type","text/json; Charset=UTF-8")
.post(body)
.build();
String str_response="";
Response response;
try {
response = client.newCall(request).execute();
try {
str_response = response.body().string();
System.out.println("POST_BODY_RESPONSE: "+" url "+url+" strresp "+str_response);
} catch (Exception ex){
Log.e("POST_BODY_RESPONSE: ",url+" resp "+str_response);
}
} catch (SocketTimeoutException e) {
// TODO Auto-generated catch block
e.printStackTrace();
str_response = "";
}catch (ConnectTimeoutException e) {
// TODO Auto-generated catch block
e.printStackTrace();
str_response = "";
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
str_response = "";
}
return str_response;
}
**Error Log:
04-30 15:06:06.518: W/System.err(5295): java.lang.IllegalArgumentException: Multiple charsets: text/json; Charset=UTF-8;charset=utf-8
04-30 15:06:06.518: W/System.err(5295): at com.squareup.okhttp.MediaType.parse(MediaType.java:64)
04-30 15:06:06.518: W/System.err(5295): at com.squareup.okhttp.Call$RealResponseBody.contentType(Call.java:252)
04-30 15:06:06.518: W/System.err(5295): at com.squareup.okhttp.ResponseBody.charset(ResponseBody.java:86)
04-30 15:06:06.518: W/System.err(5295): at com.squareup.okhttp.ResponseBody.string(ResponseBody.java:82)
04-30 15:06:06.528: W/System.err(5295): at android.app.utils.OkHttp.post(OkHttp.java:78)
04-30 15:06:06.528: W/System.err(5295): at android.app.fragment.RechercheResult$2.run(RechercheResult.java:306)
04-30 15:06:06.538: W/System.err(5295): at java.lang.Thread.run(Thread.java:856)**