Android使用okhttp 响应Post请求 使用线程


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"/>



免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM