Android Studio OkHttpClient使用教程詳解
本次來(lái)記錄下OkHttpClient的使用,OkHttpClient是用來(lái)完成android 客戶端對(duì)服務(wù)端請(qǐng)求的工具。
首先記住,使用網(wǎng)絡(luò)的時(shí)候一定要加入權(quán)限,加入到AndroidMainfest.xml中
<uses-permission android:name="android.permission.INTERNET" />
在初次使用的時(shí)候會(huì)出現(xiàn)報(bào)錯(cuò)。cannot resolve symbol OkHttpClient
這里需要引入
implementation 'com.squareup.okhttp3:okhttp:3.0.1'
然后刷新下項(xiàng)目就可以了。
代碼:
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();
}
/**
* 發(fā)送一個(gè)表單請(qǐng)求
* @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 請(qǐng)求
* 發(fā)送一個(gè)string請(qǐng)求
* @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 請(qǐng)求
* 發(fā)送一個(gè)From請(qǐng)求
* @throws Exception
*/
public void SendPostFrom() throws Exception {
RequestBody body = new FormBody.Builder()
.add("name", "sy")//添加參數(shù)體
.add("age", "18")
.build();
Request request = new Request.Builder()
.post(body) //請(qǐng)求參數(shù)
.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請(qǐng)求
* 發(fā)送一個(gè)From請(qǐng)求
* @throws Exception
*/
public void SendGetFrom() throws Exception {
Request request = new Request.Builder()
.get() //請(qǐng)求參數(shù)
.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);
}
}
測(cè)試發(fā)現(xiàn),上面的用不了,下面放一個(gè)測(cè)試通過(guò)的方法:
public void getDatasyncFactory(){
new Thread(new Runnable() {
@Override
public void run() {
try {
OkHttpClient client = new OkHttpClient();//創(chuàng)建OkHttpClient對(duì)象
Request request = new Request.Builder()
.url("http://123.207.70.54:8080/SpringMvc/hello")//請(qǐng)求接口。如果需要傳參拼接到接口后面。
.build();//創(chuàng)建Request 對(duì)象
Response response = null;
response = client.newCall(request).execute();//得到Response 對(duì)象
if (response.isSuccessful()) {
Log.d("kwwl","response.code()=="+response.code());
Log.d("kwwl","response.message()=="+response.message());
Log.d("kwwl","res=="+response.body());
//此時(shí)的代碼執(zhí)行在子線程,修改UI的操作請(qǐng)使用handler跳轉(zhuǎn)到UI線程。
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
返回信息:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java操作FreeMarker模板引擎的基本用法示例小結(jié)
這篇文章主要介紹了Java操作FreeMarker模板引擎的基本用法示例小結(jié),FreeMarker本身由Java寫成,用模板來(lái)生成文本輸出,需要的朋友可以參考下2016-02-02
Android設(shè)置Activity背景為透明style的簡(jiǎn)單方法(必看)
下面小編就為大家?guī)?lái)一篇Android設(shè)置Activity背景為透明style的簡(jiǎn)單方法(必看)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10
Android編程使用內(nèi)容提供者方式(ContentProvider)進(jìn)行存儲(chǔ)的方法
這篇文章主要介紹了Android編程使用內(nèi)容提供者方式進(jìn)行存儲(chǔ)的方法,涉及Android內(nèi)容提供者的創(chuàng)建,配置及針對(duì)數(shù)據(jù)的增刪改查等操作技巧,需要的朋友可以參考下2016-01-01
Android項(xiàng)目中引入aar包的正確方法介紹
生成aar之后下一步就是如何引用本地的aar文件,下面這篇文章主要給大家介紹了關(guān)于Android項(xiàng)目中引入aar包的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
Android簡(jiǎn)單音樂(lè)播放實(shí)例
這篇文章主要介紹了Android簡(jiǎn)單音樂(lè)播放實(shí)例,詳細(xì)介紹了Android Service使用方法2015-12-12

