JSONObject obj = new JSONObject();
obj.put("uname",uname);
obj.put("pwd",pwd);
GetPostUtil gp = new GetPostUtil();
String res = gp.post("http://192.168.0.102:8000/dologin/", obj.toString());
提交成功后
POST:,
服务器端的post打印出来,怎么整个字符串都做一个键的值了。这个是怎么回事,那位大侠知道
构造RequestBody的时候,指定JSON类型
RequestBody body = RequestBody.create(JSON, obj.toString());
指定了的
OkHttpClient client = new OkHttpClient();
public static final MediaType JSON
= MediaType.parse("application/json; charset=utf-8");
private String result=null;
public String post(String url, String json) throws IOException {
Log.i("json","--------"+json);
//把请求的内容字符串转换为json
RequestBody body = RequestBody.create(JSON, json);
Log.i("json","--------"+body);
//RequestBody formBody = new FormEncodingBuilder()
final Request request = new Request.Builder()
.url(url)
.post(body)
.build();
final Call call = client.newCall(request);
Response response = call.execute();
if(response.isSuccessful()){
result = response.body().string();
}else{
throw new IOException("Unexpected code " + response);
}
Log.i("result","--------"+result);
return result;
}