OkHttp get请求 和post请求
来源:互联网 发布:mac双系统移除win8后 编辑:程序博客网 时间:2024/05/20 05:44
//依赖
compile 'com.squareup.okhttp3:okhttp:3.9.0'
//网络请求
<uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
//布局
<Button android:id="@+id/btn_okhttp_get" android:text="Get请求" android:layout_width="match_parent" android:layout_height="wrap_content" /><Button android:id="@+id/btn_okhttp_post" android:text="Post请求" android:layout_width="match_parent" android:layout_height="wrap_content" />
.//
MainActivity
package com.example.ok;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;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 implements View.OnClickListener { private static final String TAG = "MainActivity"; private View btnGet; private View btnPost; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initview(); setListener(); } //监听事件 private void setListener(){ btnGet.setOnClickListener(this); btnPost.setOnClickListener(this); } private void initview() { btnGet =findViewById(R.id.btn_okhttp_get); btnPost =findViewById(R.id.btn_okhttp_post); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn_okhttp_get: //1OkHttpClient对象 这两种都可以 //OkHttpClient client=new OkHttpClient(); OkHttpClient client=new OkHttpClient.Builder().build(); //2Request对象 Request request=new Request.Builder() .get()//不加get 默认是get .url("http://www.wuxirui.com/") .build(); //3Call对象 Call call = client.newCall(request); //4网络请求 call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Log.e(TAG, "onFailure: "+e.getMessage()); } @Override public void onResponse(Call call, Response response) throws IOException { Log.e(TAG, "onResponse:"+response.body().string() ); } }); break; //post请求 case R.id.btn_okhttp_post: //1OkHttpClient对象 这两种都可以 OkHttpClient client2=new OkHttpClient(); //提供post请求需要的body对象 FormBody body=new FormBody.Builder() .add("mobile","15910907758") .add("password","123456") .build(); //Request对象 Request request2=new Request.Builder() .post(body) .url("http://120.27.23.105/user/login") .build(); //4call对象 Call call2 = client2.newCall(request2); //5进行网络请求 call2.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Log.e(TAG, "onFailure: "+e.getMessage()); } @Override public void onResponse(Call call, Response response) throws IOException { Log.i(TAG, "onResponse: "+response.body().string()); } }); break; } }}
阅读全文
0 0
- OkHttp get请求 和post请求
- OKhttp的get请求和post请求
- okHttp post&get请求
- okhttp get post请求
- okhttp GET请求 post请求
- OkHttp Get请求/Post请求
- Okhttp和Retrofit的get,post请求
- Okhttp的post和get请求数据
- OKHttp异步get,post请求和同步请求
- Okhttp 异步get post 请求
- okhttp 第三方 网络数据请求 异步get请求 和异步post请求 同步请求
- OkHttp使用Get和Post两种请求方式
- okhttp网络框架的使用get和post请求
- 封装OKHttp的get和post请求模版
- get请求和post请求
- get请求和post请求
- GET请求和POST请求
- GET请求和POST请求
- is 和 == 区别
- vim常用快捷键
- Unity3d入门基础之UGUI-基本控件的学习
- 51nod 1302 矩形面积交 (不要把题目玩工业了。。)
- ClassLoader的原理和应用
- OkHttp get请求 和post请求
- Ubuntu 16.04 LTS 安装RealSense R200 驱动以及SDK配置+QT+OpenCV
- 玩转SpringBoot
- 剑指offer------回溯法
- 11月的雨
- 1025. 反转链表 (25)
- 1用switch语句从控制台输入100以内的分数判断优秀,良好,及格不及格属于哪一个区间
- JZOJ 5440. 【NOIP2017提高A组冲刺11.1】背包
- [笔记]eclipse保存包含有中文字符的源代码文件出错[了解字符集]