標籤: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請求
- 基於Http的檔案上傳
- 檔案下載
- 載入圖片
- 支援要求回調,直接返回對象、對象集合
- 支援session的保持
四、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()); } };}MainActivity
布局
<?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>
activity_main
六、添加網路許可權
<uses-permission android:name="android.permission.INTERNET" />
七、運行
OkHttp的使用