GET请求基本步骤一:
1.在App/build.gradle或者Module/build.gradle中加入依赖:
compile 'com.squareup.okhttp:okhttp:2.4.0' 然后点击同步,同步的过程会下载 OkHttp的一些依赖的
2.写代码。
<?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/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="doGet" android:text="Get" /> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello World" /> </LinearLayout>
下面我们编写doGet方法
点击Get按钮的时候就会调用doGet方法。使用doGet方法访问一个HTTP网站。
对于一个请求首先需要4步:
- 拿到OkHttpClient对象。
OkHttpClient okHttpClient = new OkHttpClient();//一个全局的执行者,所有请求动作交由它执行。- 构造Request
//引入import com.squareup.okhttp.Request;的包 Request.Builder builder=new Request.Builder();//通过Builder来构造request。 Request request=builder.get().url("https://www.imooc.com/").build();//builder到build返回request- 将Request封装成Call
Call call = okHttpClient.newCall(request);//将request传入
- 执行call
/*Response response=call.execute();该方法返回值是Response,需要抛出异常*/ call.enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) {//发生错误时回调 } @Override public void onResponse(Response response) throws IOException {//执行完成回调。可以通过response进行操作 String string = response.body().string(); textView.setText(string); } });
3.简单测试:访问网络所以打开Manifest.xml加入网络权限。
<uses-permission android:name="android.permission.INTERNET"/>
编写简单的Log类。
public class L { private static final String TAG = "Imooc_okhttp";//标记 private static boolean debug=true;//开关。当你不想显示Log的时候改为false. public static void e(String msg){ if(debug){ Log.e(TAG,msg); } } }
MainActivity代码如下:
public class MainActivity extends AppCompatActivity { private TextView textView; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void doGet(View view){ textView=(TextView) findViewById(R.id.textView); OkHttpClient okHttpClient = new OkHttpClient(); Request.Builder builder=new Request.Builder(); final Request request=builder.get().url("https://www.imooc.com/").build(); Call call = okHttpClient.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { L.e("onFailure"+e.getMessage()); e.printStackTrace(); } @Override public void onResponse(Response response) throws IOException { L.e("onResponse:"); //该方法的返回值是response,所以我们可以通过response拿到相关信息。 String string = response.body().string();//想拿到字符串,可以从response-body-string /*String a="111";*/ L.e(string); /*L.e(a); */ } }); } } 如果我们直接把textView.setText(string);放入onResponse中,则会出现CalledFromWrongThreadException异常。 原因是onResponse在子线程里,控制界面UI应放在UI/主线程中。 下面的代码应写在onResponse子线程中,支持大文件下载(主线程不行)。一些基础知识哦。
//即使是一个文件我们这里也可以IO操作。容量也就是我们一个buffer大小。这样就支持大文件下载
InputStream is=response.body().byteStream();
以下用线程显示文字并把文字显示在textview中。使用线程既方便又节省代码对不对??
public class MainActivity extends AppCompatActivity { public TextView textView; public String string; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void doGet(View view){ textView=(TextView) findViewById(R.id.textView); OkHttpClient okHttpClient = new OkHttpClient(); Request.Builder builder=new Request.Builder(); final Request request=builder.get().url("https://www.imooc.com/").build(); Call call = okHttpClient.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { L.e("onFailure"+e.getMessage()); e.printStackTrace(); } @Override public void onResponse(Response response) throws IOException { L.e("onResponse:"); //该方法的返回值是response,所以我们可以通过response拿到相关信息。 string = response.body().string();//想拿到字符串,可以从response-body-string runOnUiThread(new Runnable() { @Override public void run() { textView.setText(string); } }); } }); } }
GET请求基本步骤二:
接下来我们就讲解以下细节问题咯。相信看完步骤一,大家已经把步骤的使用已经基本了解了,其他就是配置信息。1.比如说一个网络访问框架,想设置一些超时时间,发生错误的时候能不能retry,这样的一些方法就取决于全局的执行者里。按ctrl键点击OkHttpClient,里面会有很多全局的设置信息可以通过OkHttpClient的set方法进行设置。2.构造request.Builder也有一些方法。Request中也有一些参数和方法。那么具体的大家快去看源码把。3.接下来将request换成一个call对象,Call是单个请求的执行者。Call有execute()方法,与之对应就有cancel()、isCanceled()方法。4.最后执行回调。如果是异步的话有Callback(),同步的话有execute().有关文章:https://blog.csdn.net/fightingXia/article/details/70947701
https://blog.csdn.net/qq_35270692/article/details/72229753