Okhttp的get和post请求
2018-01-28 18:14
295 查看
添加okhttp和oki的依赖或者导入jar包
okhttp官网:https://github.com/square/okhttp添加联网权限
布局:点击按钮获取数据展示到TextView上
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.bwie.okhttpdemo.MainActivity"> <Button android:id="@+id/btn_get_post" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="get和post请求" /> <TextView android:id="@+id/tv_result" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="显示请求数据" /> </LinearLayout>
主界面
/** * Okhttp3原生get和post请求获取网络数据 */ public class MainActivity extends AppCompatActivity implements View.OnClickListener { // 一些配置,可以省略 public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); private static final int GET = 1; private static final int POST = 2; private Button btn_get_post; private TextView tv_result; private OkHttpClient client = new OkHttpClient(); private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case GET: // 获取数据 tv_result.setText((String) msg.obj); break; case POST: // 获取数据 tv_result.setText((String) msg.obj); break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_get_post = findViewById(R.id.btn_get_post); tv_result = findViewById(R.id.tv_result); // 设置点击事件 btn_get_post.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_get_post: // 使用原生的okhttp请求网络数据,get和post请求 tv_result.setText("");// 请求之前清空 /** * 两个请求方法二选一,都能获取到数据 */ // Get请求 // getDataFromGet();// 自动生成方法,Alt+Shift+M // Post请求 getDataFromPost(); break; } } /** * 使用get在子线程中请求网络数据 */ private void getDataFromGet() { new Thread() { @Override public void run() { super.run(); try { String result = get("http://api.m.mtime.cn/PageSubArea/TrailerList.api"); Log.e("TAG", result); Message msg = Message.obtain(); msg.what = GET; msg.obj = result; handler.sendMessage(msg); } catch (IOException e) { e.printStackTrace(); } } }.start(); } /** * 使用post在子线程中请求网络数据 */ private void getDataFromPost() { new Thread() { @Override public void run() { super.run(); try { String result = post("http://api.m.mtime.cn/PageSubArea/TrailerList.api", ""); Log.e("TAG", result); Message msg = Message.obtain(); msg.what = POST; msg.obj = result; 4000 handler.sendMessage(msg); } catch (IOException e) { e.printStackTrace(); } } }.start(); } /** * get请求 * * @param url * @return */ private String get(String url) throws IOException { Request request = new Request.Builder() .url(url) .build(); Response response = client.newCall(request).execute(); return response.body().string(); } /** * post请求 * * @param url * @param json * @return * @throws IOException */ private String post(String url, String json) throws IOException { RequestBody body = RequestBody.create(JSON, json); Request request = new Request.Builder() .url(url) .post(body) .build(); Response response = client.newCall(request).execute(); return response.body().string(); } }
相关文章推荐
- OkHttp的get post 以及同步请求Demo
- okhttp框架的最简单的基本使用 post和get请求以及用gson解析请求的数据
- OKHttp异步get,post请求和同步请求
- okhttp get post请求
- RxAndroid结合OkHttp实现图片下载、get、post请求
- okhttp GET请求 post请求
- OkHttp学习(1)-->>同步和异步(get、post键值对、post带map、请求头体封装json)
- Okhttp的post和get请求数据
- OkHttp的异步get, post请求
- [置顶] okhttp 网络请求添加请求头,get和post请求,表单提交 ,二进制流
- OkHttp的get,post请求,拦截器
- OkHttp的POST、GET请求方式
- okHttp post&get请求
- Android中使用OkHttp包处理HTTP的get和post请求的方法
- okhttp3简单封装GET和POST请求工具类
- OkHttp get post请求的封装
- OKhttp的get请求和post请求
- Android okHttp网络请求之Get/Post请求(一)
- Android OKHttp同步get请求,同步post请求,异步get请求,异步post请求
- Android 单例模式 封装 Okhttp + RxJava 网络请求(get和post)