Android 中OKHttp请求数据get和post
1:在Android Studio 的 build.gradle下 添加 然后再同步一下
compile 'com.squareup.okhttp:okhttp:2.4.0'
compile 'com.squareup.okio:okio:1.5.0'
compile 'junit:junit:4.12' 如:黄色部分
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.github.bumptech.glide:glide:3.6.1'
compile files('libs/gson-2.2.4.jar')
compile 'com.squareup.okhttp:okhttp:2.4.0'
compile 'com.squareup.okio:okio:1.5.0'
compile 'junit:junit:4.12'
}
2:get请求
如:
/**
* OkHttpClient 的Get请求
*/
//创建okHttpClient对象
OkHttpClient mOkHttpClient = new OkHttpClient();
//创建一个Request
final Request request = new Request.Builder()
.url("http://m.yunifang.com/yunifang/mobile/goods/getall?random=97575&encode=f1c61556b05c0cf193e74b1e8fc6420c&category_id=17")
.build();
//new call
Call call = mOkHttpClient.newCall(request);
//请求加入调度
call.enqueue(new Callback()
{
@Override
public void onFailure(Request request, IOException e)
{
}
@Override
public void onResponse(final Response response) throws IOException
{
String htmlStr = response.body().string();
Log.i("kkkkkkkkkkkkk",htmlStr); }
});
3:post请求 注意:有的接口不支持post请求
如:
/**
* OKHttp Post请求
*/
//创建okHttpClient对象
OkHttpClient mOkHttpClient = new OkHttpClient();
FormEncodingBuilder builder = new FormEncodingBuilder();
builder.add("random","97575");
builder.add("encode","f1c61556b05c0cf193e74b1e8fc6420c");
builder.add("category_id","17"); //创建一个Request
//http://www.yulin520.com/a2a/impressApi/news/mergeList?sign=C7548DE604BCB8A17592EFB9006F9265&pageSize=10&gender=2&ts=1871746850&page=1
//http://m.yunifang.com/yunifang/mobile/goods/getall?random=97575&encode=f1c61556b05c0cf193e74b1e8fc6420c&category_id=17
final Request request = new Request.Builder()
.url("http://m.yunifang.com/yunifang/mobile/goods/getall")
.post(builder.build()).build();
//new call
Call call = mOkHttpClient.newCall(request);
//请求加入调度
call.enqueue(new Callback()
{
@Override
public void onFailure(Request request, IOException e)
{
// Toast.makeText(MainActivity.this,"请求失败",Toast.LENGTH_SHORT).show(); } @Override
public void onResponse(final Response response) throws IOException
{
String htmlStr = response.body().string();
Log.i("htmlStr---",htmlStr); Gson gson=new Gson();
Gods gods=gson.fromJson(htmlStr,Gods.class);
databean=gods.getData();
if (adapter==null){
adapter=new MyAdapter(databean,MainActivity.this);
gv.setAdapter(adapter);
}
adapter.notifyDataSetChanged();
}
});
Android 中OKHttp请求数据get和post的更多相关文章
- angular开发中对请求数据层的封装
代码地址如下:http://www.demodashi.com/demo/11481.html 一.本章节仅仅是对angular4项目开发中数据请求封装到model中 仅仅是在项目angular4项目 ...
- Android 中 OkHttp 三步实现生命周期绑定
简介 OkHttps 是 OkHttp 增强版的超轻量封装包. 和 Retrofit 相比,它更加轻量(只有 59Kb),是 Retrofit (124Kb)的一半,而且更加的开箱即用,API 更加自 ...
- struts中的请求数据自动封装
Struts 2框架会将表单的参数以同名的方式设置给对应Action的属性中.该工作主要是由Parameters拦截器做的.而该拦截器中已经自动的实现了String到基本数据类型之间的转换工作.在st ...
- android中共享全局数据的方法
转自:http://blog.csdn.net/ameyume/article/details/6100756 我们在平时的开发中,有时候可能会需要一些全局数据,来让应用中的所有Activity和Vi ...
- 接口测试中读取excel中的请求数据含有中文问题,UnicodeEncodeError: 'latin-1' codec can't encode character '\u5c0f' in position
错误信息:UnicodeEncodeError: 'latin-1' codec can't encode character '\u5c0f' in position 31: Body ('小') ...
- Android中XML格式数据的简单使用
源码: package com.wangzhu.demo; import java.io.IOException; import java.io.StringWriter; import javax. ...
- Android(java)学习笔记208:Android中操作JSON数据(Json和Jsonarray)
1.Json 和 Xml JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的 ...
- Android中activity保存数据和状态在哪个方法实现
以前只知道在Activity销毁之前,要把数据保存在 onSaveInstanceState(Bundle)方法中,后来学习了别人的微博,学到了很多细节问题,所以整理了一下,希望能帮到大家. 如果看官 ...
- Android中获取网络数据时的分页加载
//此实在Fragment中实现的,黄色部分为自动加载,红色部分是需要注意的和手动加载, 蓝色部分是睡眠时间,自我感觉不用写 ,还有就是手动加载时,不知道为什么进去后显示的就是最后一行,求大神 ...
随机推荐
- lnmp一键安装包配置laravel项目
laravel一键安装包:https://lnmp.org/install.html 在server中加入 location / { try_files $uri $uri/ /index.php?$ ...
- Sublime的Package Control的安装
最近在用Sublime,我想很多人和我一样都是先要安装PackageControl吧! 可是看了网上的好多博客感觉都太繁琐了 对于像我这样的小白来说实在有很多看不懂的地方 相对来说还是官网的那种方法更 ...
- {java学习}[第一章]软件开发概述(2017.1.11).
- Spring MVC(二)
spring mvc工作流 1A)客户端发出http请求,只要请求形式符合web.xml 文件中配置的*.action的话,就由DispatcherServlet 来处理. 1B)Dispatcher ...
- Ubuntu 软件 安装 下载 及更新
1 软件安装 sudo apt-get install 2 软件搜索 sudo apt-cache search 3 系统已经安装了什么软件 dpkg -l 是否确切安装了某软件 dpkg ...
- AngularJs应用
引用angularjs文件 AngularJS 应用组成如下:View(视图), 即 HTML.Model(模型), 当前视图中可用的数据.Controller(控制器), 即 JavaScript ...
- Vue.js 组件笔记
Vue 组件总结笔记 一. 创建组件 1. 使用组件三部曲 ( 创建, 注册, 使用 ) 2. 全局组件:Vue.component('tag', {}); 3. 局部组件: components 属 ...
- javascript 拖放效果
最近一直在看javascript的书籍,有些东西在书上看着貌似理解了,但是在真正动手实践时,其实有些细节你根本不了解.所以看起来就算是一个简单的效果,写起来也未必简单,就算写起来简单,写的代码也未必规 ...
- sql 取2个日期之间的数据
select * from table1 where larq between(to_date('2008-9-3','yyyy-mm-dd')) and (to_date('2008-9-5','y ...
- input编辑框编辑状态切换
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...