时间:2021-05-21
本次来记录下OkHttpClient的使用,OkHttpClient是用来完成android 客户端对服务端请求的工具。
首先记住,使用网络的时候一定要加入权限,加入到AndroidMainfest.xml中
<uses-permission android:name="android.permission.INTERNET" />
在初次使用的时候会出现报错。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(); } /** * 发送一个表单请求 * @throws Exception */ 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()); } /**POST 请求 * 发送一个string请求 * @throws Exception */ public void SendPostString() throws Exception { String postBody = "" + "Releases\n" + "--------\n" + "\n" + " * _1.0_ May 6, 2013\n" + " * _1.1_ June 15, 2013\n" + " * _1.2_ August 11, 2013\n"; 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()); } /**POST 请求 * 发送一个From请求 * @throws Exception */ 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); } /**Get请求 * 发送一个From请求 * @throws Exception */ 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(); }返回信息:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
下载地址如下:https://developer.android.com/sdk/installing/studio.html很显然的IntelliJIDEA的
AndroidSimpleAdapter使用详解HolderAdapter背景Android的AdapterView用的比较多,ListView,GridVie
Android中Context的使用方法详解概要:Context字面意思是上下文,位于frameworkpackage的android.content.Cont
Android中onSaveInstanceState()使用方法详解覆盖onSaveInstanceState方法,并在onCreate中检测savedIns
Android中ActivityLifecycleCallbacks的实例详解以上就是使用ActivityLifecycleCallbacks的实例,代码注释写