1.导入okhttp-2.7.5.jar和okio-1.11.0.jar包
2.Post无线程请求
public void getDataPost(){
String s = "{\"action\":\"get\",\"object\":\"light\"}";
String path = "http://dev1.cn:xxxx";
OkHttpClient client = new OkHttpClient();
RequestBody b = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),s);
Request r = new Request.Builder()
.url(path)
.post(b)
.build();
Call c = client.newCall(r);
c.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
String s = response.body().string();
System.out.println("=========================");
Log.d("response.code()==",response.code()+"");
Log.d("response.message()==",response.message());
Log.d("res==",s);
}
});
}
3.注意无线程中需要使用enqueue回调一下
4.需要在清单文件中