以前的项目网络连接那块一直坚持使用HttpClient,总是会出现一些莫名奇妙的问题,现在新的项目使用了OKHttp网络框架,发现超级好用,上网再了解下,发现OkHttp口碑真的不错,对比之下HttpClient简直就是弱爆啦,今天就OKHttp post方式提交键值对的使用提出来,一者为了总结所学的知识,然后也是为了日后项目的偷懒,OKHttp的官方介绍地址,还有OKHttp的github源码,一开始我使用的是2.3的版本,post提交键值对也没啥问题,但是在图片上传方面遇到了bug怎么都解决不掉,再加上我这人喜新厌旧,一怒之下就把OKHttp的版本升到最高的3.4.1版本,升级之后发现变化特别大,在2.3版本中有的类和方法在3.4.1版本中根本就不存在啦,比如说FormEncodingBuilder()方法,真是没办法啦,只能找文档啦。

现在来说,还使用eclipse作为Android开发Android的童鞋,只能说你太怀旧啦,这里就不说eclipse怎么导入OKHttp啦,对于使用studio的童鞋来说,导入OKHttp特别简单,只需要在gradle中添加两行代码就行。

 compile 'com.squareup.okhttp3:okhttp:3.4.1'
compile 'com.squareup.okio:okio:1.9.0'

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAmgAAAJSCAIAAAD4bK7VAAAgAElEQVR4nO3dz+/91l3n8fN/IA1LZptVzrosyI4F0pQGISjNPRRG6mKkWTHKvZWokCbUi5H6YQGbSEyFyAeYhs/pIDEN+jQtogOj+c7mEykWQ5RI34pPRjRAkuabQj0L28fnp+3j63t97ft8yPrm8/HXPj73fj7x6/v28fUR/wUAAIwmPnA9efKkAgAArXfeecf+luAEAKAPwQkAQAaCEwCADAQnAAAZCM4jabUXYq/00v3AefFzB67XmYKzLKQQQogZTjTlvRQHIQ7HnLZmPOtptRfipihnaGpeZXEz+V1KvT/HtDlK+od7iqA6ps2L/bkDOLlzVpxazRGcTVNHnUbL4maWs15Z3Aixl8XjsQ2dRt29Ce9Sz/szuc3xoj/cuX5ks7R54T93AKd1ncF5LfTt/O/SKdr0jnCKH255L/mFATALgnPoQLP1eQEE5ynbBHClpgRnO2DZRIpWQgillBBCyKKMrqmaDc2ZSyv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CADAQnAAAZCE4AADIQnAAAZCA4AQDIQHACAJCB4AQAIAPBCQBABoITAIAMBCcAABkITgAAMhCcAABkIDgBAMhAcAIAkIHgBAAgA8EJAEAGghMAgAwEJwAAGQhOAAAyEJwAAGQgOAEAyEBwAgCQgeAEACADwQkAQAaCEwCADAQnAAAZCE4AADIQnAAAZCA4AQDIQHACAJCB4AQAIAPBCQBABoITAIAMBCcAABkITgAAMhCcAABkIDgBAMhAcAIAkIHgBAAgA8EJAEAGghMAgAwEJwAAGQhOAAAyEJwAAGQgOAEAyEBwAgCQgeAEACADwQkAQAaCEwCADAQnAAAZvOD8/ydJmuI6qPuUAAAAAElFTkSuQmCC" alt="" />

我们可以在全局变量中初始化一个OKHttpClient并且配置一些比如连接时间的配置,我们就可以不用每向服务器请求一次数据就初始化一个OKHttpClient啦。

 package com.example.keranbin.okhttpdemo;

 import android.app.Application;

 import java.io.File;
import java.util.concurrent.TimeUnit; import okhttp3.Cache;
import okhttp3.OkHttpClient; /**
* Created by keranbin on 2016/7/26.
*/
public class APP extends Application {
public static OkHttpClient okHttpClient;
public static APP app; @Override
public void onCreate() {
super.onCreate();
app=this;
} public static OkHttpClient OkHttpInstance() {
if (okHttpClient == null) {
okHttpClient = new OkHttpClient().newBuilder()
.connectTimeout(500, TimeUnit.MILLISECONDS)
.readTimeout(1000,TimeUnit.MICROSECONDS)
.writeTimeout(1000,TimeUnit.MICROSECONDS)
.cache(new Cache(app.getExternalCacheDir(), 10 * 1024 * 1024))
.build();
return okHttpClient;
} else {
return okHttpClient;
}
}
}

我们不可能每请求一次数据就一个一个的往builder里面add个不停,想了下,干脆把这一块的代码提出来,调用的时候传个Map进来,一个for循环搞定,同时也把Handler传进来,那我们在Activity中不就直接可以操作请求回来的数据了吗。

 package com.example.keranbin.okhttpdemo;

 import android.os.Handler;
import android.os.Message;
import android.util.Log; import java.io.IOException;
import java.util.Map; import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response; /**
* Created by keranbin on 2016/7/26.
*/
public class HttpCon {
private static OkHttpClient okHttpClient =APP.OkHttpInstance();
private static String URL = "http://10.11.5.229:8080/kqgl/ajax.mobileSword";
/*
* @map 把参数都封装到map中
* @code 标志位,比如说handler的msg.what
* @handler
* */
public static void Params(Map<String, Object> map, final int code, final Handler handler) { Log.i("参数map",map.toString()); FormBody.Builder builder = new FormBody.Builder();
if (map != null) {
for (Map.Entry<String, Object> entry : map.entrySet()) {
Log.i("参数:" , entry.getKey() + ":" + entry.getValue());
builder.add(entry.getKey(), entry.getValue().toString());
}
}
RequestBody requestBody = builder.build();
Request request = new Request.Builder().url(URL)
.header("Cache-Control", " max-stale=600")
.post(requestBody)
.build(); okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.i("网络错误" ,e.toString());
} @Override
public void onResponse(Call call, Response response) throws IOException {
String s = response.body().string().toString();
Log.i("数据请求结果:" , s);
Message message = handler.obtainMessage();
message.what = code;
message.obj = s;
handler.sendMessage(message);
} });
}
}

再看看MainActivity中怎么调用,把封装的Map,还有Code,Handler传过来就OK,就这么简单。

 package com.example.keranbin.okhttpdemo;

 import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
import android.widget.Toast; import java.util.HashMap;
import java.util.Map; public class MainActivity extends Activity {
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 0x123:
Toast.makeText(MainActivity.this,msg.obj.toString(),Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Map map=new HashMap();
//这里只是举个例子,具体的操作要视服务器端需要的数据哈
map.put("tid","KQ_XjjbglMobileCBS_getJbInitData");
map.put("ryId","3423423243243243");
HttpCon.Params(map,0x123,handler);
}
}

Android Okhttp POST提交键值对的更多相关文章

  1. Android okHttp网络请求之Json解析

    前言: 前面两篇文章介绍了基于okHttp的post.get请求,以及文件的上传下载,今天主要介绍一下如何和Json解析一起使用?如何才能提高开发效率? okHttp相关文章地址: Android o ...

  2. Android okHttp网络请求之缓存控制Cache-Control

    前言: 前面的学习基本上已经可以完成开发需求了,但是在项目中有时会遇到对请求做个缓存,当没网络的时候优先加载本地缓存,基于这个需求我们来学习一直okHttp的Cache-Control. okHttp ...

  3. Android OkHttp完全解析 --zz

    参考文章 https://github.com/square/okhttp http://square.github.io/okhttp/ 泡网OkHttp使用教程 Android OkHttp完全解 ...

  4. Android OkHttp完全解析 是时候来了解OkHttp了

    Android OkHttp完全解析 是时候来了解OkHttp了 标签: AndroidOkHttp 2015-08-24 15:36 316254人阅读 评论(306) 收藏 举报  分类: [an ...

  5. Android OkHttp文件上传与下载的进度监听扩展

    http://www.loongwind.com/archives/290.html 上一篇文章介绍了用Retrofit实现文件的上传与下载,但是我们发现没办法监听上传下载的进度,毕竟我们在做开发的时 ...

  6. Android okHttp网络请求之Get/Post请求

    前言: 之前项目中一直使用的Xutils开源框架,从xutils 2.1.5版本使用到最近的xutils 3.0,使用起来也是蛮方便的,只不过最近想着完善一下app中使用的开源框架,由于Xutils里 ...

  7. Android okHttp网络请求之文件上传下载

    前言: 前面介绍了基于okHttp的get.post基本使用(http://www.cnblogs.com/whoislcj/p/5526431.html),今天来实现一下基于okHttp的文件上传. ...

  8. Android okHttp网络请求之Retrofit+Okhttp+RxJava组合

    前言: 通过上面的学习,我们不难发现单纯使用okHttp来作为网络库还是多多少少有那么一点点不太方便,而且还需自己来管理接口,对于接口的使用的是哪种请求方式也不能一目了然,出于这个目的接下来学习一下R ...

  9. Android keyevent 中的各个值

    Android keyevent 中的各个值,在使用adb shell input 的时候用得到. 是从http://blog.csdn.net/huiguixian/article/details/ ...

随机推荐

  1. UiPath:Log Message: Column 'user_role' does not belong to table .非说这个列不存在,记录一下看看怎么解决

    如图 确实是不存在...因为Excel的取值范围没有囊括user_role列...

  2. 【入门篇一】HelloWorld演示(2)

    一.传统使用 Spring 开发一个“HelloWorld”的 web 应用 1. 创建一个 web 项目并且导入相关 jar 包. 2. 创建一个 web.xml 3. 编写一个控制类(Contro ...

  3. 微信小程序 - 双线程模型

    小程序的双线程模型 官方文档给出的双线程模型: 小程序的宿主环境 微信客户端提供双线程去执行wxml,wxss,js文件. 双线程模型 1.上述的渲染层上面运行着wxml文件,渲染层使用是的webvi ...

  4. c语言命名规范图

    来自armbbs

  5. requests--会话对象,ssl验证

    会话对象 前面我们使用了添加cookie的方式来进行接口的访问,如果有几十个接口都要依赖登录,难道我们都要加上吗? Request的会话对象让你能够跨请求保持某些参数,它也会在同一请求Session实 ...

  6. [LeetCode] 662. Maximum Width of Binary Tree 二叉树的最大宽度

    Given a binary tree, write a function to get the maximum width of the given tree. The width of a tre ...

  7. [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索之二

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  8. WC 2008 观光计划(斯坦纳树)

    题意 https://www.lydsy.com/JudgeOnline/problem.php?id=2595 思路 是一道比较裸的斯坦纳树呢- 题意等价于选出包含一些点的最小生成树,这就是斯坦纳树 ...

  9. Git基础-第2章

    简单的Git基础概念: repository: 仓库 track:  跟踪 stage: 暂存 commit:    提交 push:        推送 pull:    拉取 一.获取Git仓库 ...

  10. FFT(快速傅里叶变换)

    FFT(快速傅里叶变换) 前置知识 \(1.复数\) \(2.单位根\) \(3.循环结构\) \(4.C++\) 1.复数 \(定义:形如a+bi的数,其中i^2=-1\) \(计算:1.(a+bi ...