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中实现的,黄色部分为自动加载,红色部分是需要注意的和手动加载, 蓝色部分是睡眠时间,自我感觉不用写 ,还有就是手动加载时,不知道为什么进去后显示的就是最后一行,求大神 ...
随机推荐
- jsp或servlet返回并刷新页面
2012-04-27 22:39:05| 分类: JAVA | 标签:返回刷新 |举报|字号 订阅 只用window.history.back(-1);返回页面但不会刷新,还是原来的数据 ...
- 使用Image.GetThumbnailImage 方法返回缩略图
如果 Image 包含一个嵌入式缩略图像,则此方法会检索嵌入式缩略图,并将其缩放为所需大小. 如果 Image 不包含嵌入式缩略图像,此方法会通过缩放主图像创建一个缩略图像. 请求的缩略图像大小为 1 ...
- c# 配置文件App.config操作类库
public class ConfigOperator { #region 从配置文件获取Value /// <summary> /// 从配置文件获取Value /// </sum ...
- Linux Kernel Vhost 架构
Vhost 回顾 Linux中的vhost驱动程序提供了内核virtio设备仿真. 通常,QEMU用户空间进程模拟guest的I / O访问. Vhost将virtio仿真代码放到内核中,使QEMU用 ...
- React学习笔记-03 state
每一个组件都有状态,比如一个开关,开 和 关,都是一种state.那么react是怎么管理它的state的? React 把用户界面当做状态机,可以轻松的让用户界面和数据保持一致.用户只需要更新组件的 ...
- 《JavaScript高级程序设计》读书笔记 ---Function 类型
说起来ECMAScript 中什么最有意思,我想那莫过于函数了——而有意思的根源,则在于函数实际上是对象.每个函数都是Function 类型的实例,而且都与其他引用类型一样具有属性和方法.由于函数是对 ...
- Oracle杀死死锁进程
查杀系统死锁的sql,最近想改造成存储过程,如下: CREATE OR REPLACE PROCEDURE HERO_KILLLOCKSESSION (OUT_COUNT OUT NUMBER, OU ...
- CentOS6.3配置SVN之subversion1.7.7
今天配置了 SVN 记在这儿 备忘: 1. 下载 svn 软件包和依赖包 cd /mydata/soft wget http://labs.mop.com/apache-mirror/subversi ...
- nginx: [emerg] socket() [::]:80 failed (97: Address family not supported by protocol)
环境:Centos6.5 行为:安装nginx 问题: nginx: [emerg] socket() [::]: failed (: Address family not supported by ...
- linux jdk环境变量
export JAVA_HOME=/usr/share/jdk8 export PATH=$JAVA_HOME/bin:$PATH export CLASSPATH=.:$JAVA_HOME/lib/ ...