1.在libs中导入okhttp-2.7.5.jar和okio-1.11.0.jar。
2.post请求
public void getData(){
new Thread(new Runnable() {
@Override
public void run() {
try {
String s = "{\"action\":\"get\",\"object\":\"light\"}";
OkHttpClient client = new OkHttpClient();
RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),s);
Request request = new Request.Builder()
.post(body)
.url("http://dev1.cn:xxxx")
.build();
Response response = null; response = client.newCall(request).execute();
if (response.isSuccessful()){
Log.d("response.code()==",response.code()+"");
Log.d("response.message()==",response.message());
Log.d("res==",response.body().string());
}
} catch (Exception e) {
e.printStackTrace();
} }
}).start();
};
3.在onCreate 中默认界面启动
4.需要在清单文件中
<uses-permission android:name="android.permission.INTERNET"/>