标签:bre onclick public mis ora builder 使用 schema textview
一、Android主流网络请求开源库的对比(Android-Async-Http,Volley,OkHttp,Retrofit)
来自大神博客:http://blog.csdn.net/carson_ho/article/details/52171976
二、站在巨人的肩膀上
张鸿洋大神的博客:http://blog.csdn.net/lmj623565791/article/details/47911083
OkHttpUtils的GitHub地址:https://github.com/hongyangAndroid/okhttputils
三、okhttp的简单使用,主要包含:
四、get和post请求简单demo
下载OkHttp和Okio两个jar包并导入新建工程
OkHttp:https://github.com/square/okhttp
Okio:https://github.com/square/okio
五、代码:
package com.yuanlei.okthhpdemo; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.io.IOException; import okhttp3.Call; import okhttp3.Callback; import okhttp3.FormBody; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class MainActivity extends AppCompatActivity { private Button btnGet, btnPost; private TextView tvContent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnGet = (Button) findViewById(R.id.btn_get); btnPost = (Button) findViewById(R.id.btn_post); tvContent = (TextView) findViewById(R.id.tv_content); btnGet.setOnClickListener(myOnClickListener); btnPost.setOnClickListener(myOnClickListener); } private void doGet() { //1、拿到OkHttpClient对象 OkHttpClient okHttpClient = new OkHttpClient(); //2、构造Request //使用聚合数据的号码归属地接口 //http://apis.juhe.cn/mobile/get?phone=13666666666&key=6dee1e2355424eb4d1949b2d7037bfc6 Request.Builder builder = new Request.Builder(); Request request = builder.get().url("http://apis.juhe.cn/mobile/get?phone=13666666666&key=6dee1e2355424eb4d1949b2d7037bfc6").build(); //3、将Request封装为Call Call call = okHttpClient.newCall(request); //4、执行 execute(call); } private void doPost() { //1、拿到OkHttpClient的对象 OkHttpClient okHttpClient = new OkHttpClient(); //2、构造Request //使用聚合数据的号码归属地接口 //http://apis.juhe.cn/mobile/get?phone=13666666666&key=6dee1e2355424eb4d1949b2d7037bfc6 //okhttp3.FormBody instead of FormEncodingBuilder.(OkHttp3.x,FormEncodingBuilder已被FormBody取代) FormBody formBody = new FormBody.Builder() .add("phone", "13666666666") .add("key", "6dee1e2355424eb4d1949b2d7037bfc6") .build(); Request request = new Request.Builder() .url("http://apis.juhe.cn/mobile/get?") .post(formBody) .build(); //3、将Request封装为Call Call call = okHttpClient.newCall(request); //4、执行 execute(call); } private void execute(Call call) { //1、直接执行得到响应结果response // Response response = call.execute(); //2、异步执行 call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Log.d("onFailure", e.getMessage()); e.printStackTrace(); } @Override public void onResponse(Call call, Response response) throws IOException { String res = response.body().string(); Log.d("onResponse:", res); Message msg = new Message(); msg.obj = res; handler.sendMessage(msg); } }); } private View.OnClickListener myOnClickListener = new View.OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_get: doGet(); break; case R.id.btn_post: doPost(); break; } } }; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { tvContent.setText(msg.obj.toString()); } }; }
布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/btn_get" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Get" android:textAllCaps="false" /> <Button android:id="@+id/btn_post" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Post" android:textAllCaps="false" /> <TextView android:id="@+id/tv_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="返回结果" /> </LinearLayout>
六、添加网络权限
<uses-permission android:name="android.permission.INTERNET" />
七、运行效果图
标签:bre onclick public mis ora builder 使用 schema textview
原文地址:http://www.cnblogs.com/bky1225987336/p/6414699.html