栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android Studio OkHttpClient使用教程详解

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android Studio OkHttpClient使用教程详解

本次来记录下OkHttpClient的使用,OkHttpClient是用来完成android 客户端对服务端请求的工具。

首先记住,使用网络的时候一定要加入权限,加入到AndroidMainfest.xml中


在初次使用的时候会出现报错。cannot resolve symbol OkHttpClient

这里需要引入

implementation 'com.squareup.okhttp3:okhttp:3.0.1'
然后刷新下项目就可以了。

代码:

package com.example.administrator.testclient;


import com.squareup.*;

import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class baseHttpClient {

 public static final MediaType MEDIA_TYPE_MARKDOWN
   = MediaType.parse("text/x-markdown; charset=utf-8");
 // 01. 定义okhttp
 private final OkHttpClient client = new OkHttpClient();

 public baseHttpClient(){

  //client.connectTimeoutMillis();
 }


 
 public void SendForm() throws Exception {
  RequestBody formBody = new FormBody.Builder()
    .add("search", "Jurassic Park")
    .build();
  Request request = new Request.Builder()
    .url("https://en.wikipedia.org/w/index.php")
    .post(formBody)
    .build();

  Response response = client.newCall(request).execute();
  if (!response.isSuccessful())
   throw new IOException("Unexpected code " + response);

  System.out.println(response.body().string());
 }

 
 public void SendPostString() throws Exception {
  String postBody = ""
    + "Releasesn"
    + "--------n"
    + "n"
    + " * _1.0_ May 6, 2013n"
    + " * _1.1_ June 15, 2013n"
    + " * _1.2_ August 11, 2013n";

  Request request = new Request.Builder()
    .url("https://api.github.com/markdown/raw")
    .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, postBody))
    .build();

  Response response = client.newCall(request).execute();
  if (!response.isSuccessful())
   throw new IOException("Unexpected code " + response);

  System.out.println(response.body().string());
 }

 
 public void SendPostFrom() throws Exception {

  RequestBody body = new FormBody.Builder()
    .add("name", "sy")//添加参数体
    .add("age", "18")
    .build();

  Request request = new Request.Builder()
    .post(body) //请求参数
    .url("http://123.207.70.54:8080/SpringMvc/hello")
    .build();

  Response response = client.newCall(request).execute();
  if (!response.isSuccessful())
   throw new IOException("Unexpected code " + response);
 }

 
 public void SendGetFrom() throws Exception {

  Request request = new Request.Builder()
    .get() //请求参数
    .url("http://123.207.70.54:8080/SpringMvc/hello")
    .build();

  Response response = client.newCall(request).execute();
  if (!response.isSuccessful())
   throw new IOException("Unexpected code " + response);
 }

}

测试发现,上面的用不了,下面放一个测试通过的方法:

public void getDatasyncFactory(){
    new Thread(new Runnable() {
     @Override
     public void run() {
      try {
OkHttpClient client = new OkHttpClient();//创建OkHttpClient对象
Request request = new Request.Builder()
  .url("http://123.207.70.54:8080/SpringMvc/hello")//请求接口。如果需要传参拼接到接口后面。
  .build();//创建Request 对象
Response response = null;
response = client.newCall(request).execute();//得到Response 对象
if (response.isSuccessful()) {
 Log.d("kwwl","response.code()=="+response.code());
 Log.d("kwwl","response.message()=="+response.message());
 Log.d("kwwl","res=="+response.body());
 //此时的代码执行在子线程,修改UI的操作请使用handler跳转到UI线程。
}
      } catch (Exception e) {
e.printStackTrace();
      }
     }
    }).start();
   }

返回信息:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/153825.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号