android 中okhttp post请求传递json数据
来源:互联网 发布:2017卫浴市场数据 编辑:程序博客网 时间:2024/05/18 21:42
参考地址 http://blog.csdn.net/lmj623565791/article/details/47911083
client 基础配置
public final static int CONNECT_TIMEOUT = 60;public final static int READ_TIMEOUT = 100;public final static int WRITE_TIMEOUT = 60;public static final OkHttpClient client = new OkHttpClient.Builder() .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)//设置读取超时时间 .writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS)//设置写的超时时间 .connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)//设置连接超时时间 .build();
get方法
参数:
url get请求地址
public String get(String url) throws IOException { Request request = new Request.Builder().url(url).get().build(); Response response = client.newCall(request).execute(); if (response.isSuccessful()) { return response.body().string(); } else { throw new IOException("Unexpected code " + response); }}
post方法
参数:
url post请求地址
json json字符串
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");public static String post(String url, String json) throws IOException { RequestBody body = RequestBody.create(JSON, json); Request request = new Request.Builder() .url(url) .post(body) .build(); Response response = client.newCall(request).execute(); if (response.isSuccessful()) { return response.body().string(); } else { throw new IOException("Unexpected code " + response); }}
调用:
new Thread() { @Override public void run() { //传的json JSONObject jsonObject = new JSONObject(); try { String callStr = OKHttpTool.post(HttpUrl.API_ACTIVE, jsonObject.toString()); JSONObject call_json = new JSONObject(callStr); final String msg = call_json.getString("msg"); if (call_json.getInt("status") == 1){ //在子线程中调用ui线程 runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(ActivationCardActivity.this, msg, Toast.LENGTH_SHORT).show(); finish(); } }); } } catch (IOException e) { e.printStackTrace(); } }}.start();
12 0
- android 中okhttp post请求传递json数据
- Android中post请求传递json数据给服务端
- Android异步线程OkHttp Post请求Json数据并解析
- Android异步线程OkHttp Post请求Json数据并解析
- OkHttp请求json数据
- okhttp请求json数据
- Android中Okhttp进行get和post提交及数据请求的基本使用
- Android的okhttp的post请求,php返回json数据。以及遇到的okhttp dispatcher问题,和json解析遇到的问题
- android studio中OkHttp请求数据
- android Json数据分装Post请求
- 如何处理post请求方JSON数据传递的问题
- Okhttp 向服务器发送请求(请求头,表单,post json数据)
- 关于Okhttp的post请求数据
- Okhttp的post和get请求数据
- OKhttp post异步请求获取数据
- android okHttp 发送post 、get请求示例
- Android-okhttp+fastJson数据请求
- post请求获取json数据
- android 属性动画2
- 优化android程序
- Java第二章
- 图片缓存逻辑
- 优化android程序2
- android 中okhttp post请求传递json数据
- android中的权限
- mybatis generator 的使用
- android基础
- HDU4289 水题...
- genymotion运行ARM架构程序
- Git 命令行基础
- Java模板引擎之Freemarker
- instanceof 算符是用来在运行时指出对象是否是特定类的一个实例