| android { |
| android.buildFeatures.viewBinding = true |
| } |
| |
| dependencies { |
| implementation 'com.squareup.okhttp3:okhttp:3.14.+' |
| } |
| <?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" |
| tools:context=".MainActivity" |
| android:orientation="vertical"> |
| |
| <Button |
| android:id="@+id/btn1" |
| android:layout_width="match_parent" |
| android:layout_height="wrap_content" |
| android:text="测试请求"/> |
| |
| <TextView |
| android:id="@+id/textView" |
| android:layout_width="match_parent" |
| android:layout_height="wrap_content"/> |
| |
| </LinearLayout> |
| package com.example.okhttp; |
| |
| import androidx.appcompat.app.AppCompatActivity; |
| import android.os.Bundle; |
| import android.view.View; |
| import com.example.okhttp.databinding.ActivityMainBinding; |
| import java.io.IOException; |
| import okhttp3.OkHttpClient; |
| import okhttp3.Request; |
| import okhttp3.Response; |
| |
| public class MainActivity extends AppCompatActivity { |
| |
| ActivityMainBinding binding; |
| |
| OkHttpClient okHttpclient; |
| |
| @Override |
| protected void onCreate(Bundle savedInstanceState) { |
| super.onCreate(savedInstanceState); |
| binding = ActivityMainBinding.inflate(getLayoutInflater()); |
| setContentView(binding.getRoot()); |
| |
| |
| okHttpclient = new OkHttpClient.Builder().build(); |
| |
| binding.btn1.setOnClickListener(new View.OnClickListener(){ |
| @Override |
| public void onClick(View view) { |
| |
| testGet(); |
| } |
| }); |
| } |
| |
| |
| private void testGet(){ |
| Request request = new Request.Builder().url("http://127.0.0.1:4523/m1/4635458-4285948-default/pet/1").build(); |
| |
| |
| new Thread(new Runnable(){ |
| @Override |
| public void run(){ |
| try { |
| Response response = okHttpclient.newCall(request).execute(); |
| String result = response.body().string(); |
| runOnUiThread(new Runnable() { |
| @Override |
| public void run() { |
| |
| binding.textView.setText(result); |
| } |
| }); |
| } catch (IOException e){ |
| e.printStackTrace(); |
| } |
| } |
| }).start(); |
| } |
| |
| } |
- AndroidManifest.xml中进行网络配置
| <uses-permission android:name="android.permission.INTERNET" /> |
| <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> |
| <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> |
| <application |
| android:networkSecurityConfig="@xml/network_security_config" |
| > |
-
启动虚拟环境,启动app,点击按钮测试
-
发送异步请求
| |
| private void testGet() { |
| |
| FormBody formBody = new FormBody.Builder().add("test", "这是一个测试的参数").build(); |
| Request request = new Request.Builder().url("http://192.168.0.109:6666/fc/test").post(formBody).build(); |
| |
| |
| okHttpclient.newCall(request).enqueue(new Callback() { |
| |
| @Override |
| public void onFailure(Call call, IOException e) { |
| |
| } |
| |
| @Override |
| public void onResponse(Call call, Response response) throws IOException { |
| String result = response.body().string(); |
| runOnUiThread(new Runnable() { |
| @Override |
| public void run() { |
| |
| binding.textView.setText(result); |
| } |
| }); |
| } |
| }); |
| } |
| |
| private void testGet() { |
| |
| RequestBody requestBody = RequestBody.create(MediaType. |
| parse("application/json; charset=utf-8"),"\"requestBody数据实体\""); |
| Request request = new Request.Builder().url("http://192.168.0.109:6666/fc/test2").post(requestBody).build(); |
| |
| |
| |
| okHttpclient.newCall(request).enqueue(new Callback() { |
| |
| @Override |
| public void onFailure(Call call, IOException e) { |
| |
| } |
| |
| @Override |
| public void onResponse(Call call, Response response) throws IOException { |
| String result = response.body().string(); |
| runOnUiThread(new Runnable() { |
| @Override |
| public void run() { |
| |
| binding.textView.setText(result); |
| } |
| }); |
| } |
| }); |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2022-08-05 Reactor 模式
2022-08-05 netty简介
2022-08-05 零拷贝
2022-08-05 群聊案例
2022-08-05 SelectionKey、ServerSocketChannel、SocketChannel常用api
2022-08-05 NIO案例
2022-08-05 1个main方法同时启动多次