Android封装OkHttpClient的类库
由于android6.0的SDK没有HttpClient,只有HttpURLConnection和OkHttpClient,特记下OkHttpClient的使用方法
1.Ui测试界面布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.lidezhen.httpdemo.MainActivity"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Get方式"
android:id="@+id/button1"
android:onClick="Button1_Click"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Post方式"
android:id="@+id/button2"
android:onClick="Button2_Click"/>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text1"
android:hint="这是内容"/>
</ScrollView>
</LinearLayout>
添加权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lidezhen.httpdemo">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
代码
package com.example.lidezhen.httpdemo; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView; import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response; public class MainActivity extends AppCompatActivity { TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView= (TextView) findViewById(R.id.text1); } public void Button1_Click(View v) {
new Thread(new Runnable() {
@Override
public void run() {
String url="https://www.baidu.com/";
OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder()
.url(url).build(); Response response = null;
try {
response = client.newCall(request).execute(); if (response.isSuccessful()) { SetText( response.body().string()); } else {
SetText( "Unexpected code " + response); }
}
catch (Exception e)
{
SetText( "获取网页失败");
}
} }).start(); }
public void SetText(final String s)
{
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(s);
}
});
} public void Button2_Click(View v)
{
new Thread(new Runnable() {
@Override
public void run() {
String url="https://passport.baidu.com/v2/api/?login";
OkHttpClient client = new OkHttpClient();
FormBody.Builder formBody = new FormBody.Builder();
formBody.add("username","abc123").add("password","123456");
Request request = new Request.Builder()
.post(formBody.build())
.url(url).build(); Response response = null;
try {
response = client.newCall(request).execute(); if (response.isSuccessful()) { SetText( response.body().string()); } else {
SetText( "Unexpected code " + response); }
}
catch (Exception e)
{
SetText( "获取网页失败");
}
} }).start(); }
}

Android封装OkHttpClient的类库的更多相关文章
- Android Studio导入第三方类库的方法
Android Studio导入第三方类库的方法 本人也刚刚开始尝试做android app的开发,听说android studio是Google支持的android 应用开发工具,所以想应该肯定比E ...
- Android 封装Dialog
package com.example.myandroid01; import android.support.v7.app.ActionBarActivity; import android.os. ...
- Android——Android Studio导入SlidingMenu类库的方法
Android Studio导入SlidingMenu类库的方法(其他类库应该也适用) 本篇文章主要介绍了"Android Studio导入SlidingMenu类库的方法(其他类库应该 ...
- Android封装TitleBar基本适用所有常规开发
Android封装TitleBar基本适用所有常规开发 github地址:https://github.com/SiberiaDante/SiberiaDanteLib/blob/master/sib ...
- Android Studio导入第三方类库的方法(转)
转自:链接 本人也刚刚开始尝试做android app的开发,听说android studio是Google支持的android 应用开发工具,所以想应该肯定比Eclipse好用吧,反正以前没有jav ...
- Android实战技巧之十二:Android Studio导入第三方类库、jar包和so库
第三方类库源码 将一网友的XMPP代码从ADT转到AS时,发现其使用了第三方类库,源码放在了lib下,直接在AS中Import project,第三方类库并没有自动导入进来,看来需要自己动手了. 项目 ...
- [Android Studio导入第三方类库方法] Error:(19, 23) 错误: 程序包org.apache.http不存在
本文主要参考以下链接: http://m.blog.csdn.net/blog/BoxRice/48575027 https://drakeet.me/android-studio http://ww ...
- Android Studio使用第三方类库
导入*.jar包 新建好了Android项目,添加一个第三方已经打包好的jar文件进你项目,下面就已添加一个odata4j的一个包 在项目中添加一个libs文件 直接通过COPY/PAST 把你下载的 ...
- android studio 引入第三方类库jar包
第三方类库jar包 这就简单多了,直接将jar包拷贝到app/libs下,然后在app下的build.gradle中添加此jar的依赖.如下: dependencies { compile 'com. ...
随机推荐
- 循序渐进做项目系列(4)迷你QQ篇(2)——视频聊天!(附源码)
一·效果展示 源码派送:MiniQQ1.1 文字聊天的实现参见:循序渐进做项目系列(3):迷你QQ篇(1)——实现客户端互相聊天 二·服务端设计 对于实现视频聊天而言,服务端最核心的工作就是要构造多媒 ...
- MySQL RANGE分区
200 ? "200px" : this.width)!important;} --> 介绍 RANGE分区基于一个给定的连续区间范围,早期版本RANGE主要是基于整数的分区 ...
- C# Azure 存储-分布式缓存Redis工具类 RedisHelper
using System; using System.Collections.Generic; using Newtonsoft.Json; using StackExchange.Redis; na ...
- 使用Ldoc给Lua生成文档
Ldoc介绍 LDoc是一个Lua的文档生成工具,过去,比较常用的Lua生成文档的工具是LuaDoc,可惜作者自从2008年之后就再也没有发布过新的版本了,说明作者基本上已经放弃维护了.而LDoc则是 ...
- Java Math 取整的方式
1.Math.floor floor,英文原意:地板. Math.floor 函数是求一个浮点数的地板,就是 向下 求一个最接近它的整数,它的值肯定会小于或等于这个浮点数. 2.Math.ceil c ...
- Java基础-服务器的发送和接收
package hanqi.test; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWri ...
- Apk去掉签名以及重新签名的方法
Android开发中很重要的一部就是用自己的密钥给Apk文件签名,不经过签名的Apk文件一般是无法安装的,就算装了最后也是失败. 网上流传的"勾选允许安装未知来源的应用"其实跟签不 ...
- iOS开发之微信聊天页面实现
在上篇博客(iOS开发之微信聊天工具栏的封装)中对微信聊天页面下方的工具栏进行了封装,本篇博客中就使用之前封装的工具栏来进行聊天页面的编写.在聊天页面中主要用到了TableView的知识,还有如何在俩 ...
- 理解brk和sbrk
brk和sbrk的定义 在man手册中定义了这两个函数: #include <unistd.h> int brk(void *addr); void *sbrk(intptr_t incr ...
- linux中断与异常
看了<深入理解linux内核>的中断与异常,简单总结了下,如果有错误,望指正! 一 什么是中断和异常 异常又叫同步中断,是当指令执行时由cpu控制单元产生的,之所以称之为异常,是因为只有在 ...