From 6cafdd2af46bb1415341a3e8785bdb2c0528ec60 Mon Sep 17 00:00:00 2001 From: wuhuajuan <396497877@qq.com> Date: Fri, 25 Oct 2024 15:17:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4MainActivity.java=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MainActivity.java | 135 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 MainActivity.java diff --git a/MainActivity.java b/MainActivity.java new file mode 100644 index 0000000..42368e6 --- /dev/null +++ b/MainActivity.java @@ -0,0 +1,135 @@ +package com.example.chapter9_okhttp; + +import androidx.appcompat.app.AppCompatActivity; + +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.widget.TextView; + +import org.jetbrains.annotations.NotNull; + +import java.io.File; +import java.io.IOException; + +import okhttp3.Call; +import okhttp3.Callback; +import okhttp3.FormBody; +import okhttp3.Headers; +import okhttp3.MediaType; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; + +public class MainActivity extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + } + + public void getSync(View v){ + OkHttpClient client = new OkHttpClient(); + + new Thread(){ + @Override + public void run() { +// + String url = "http://api.k8s.mt178.com/mock/orange/product/search"; + //String url = "https://www.httpbin.org/get?a=1&b=2"; + Request request = new Request.Builder() + .url(url) + .get() + .build(); + // 准备好请求的Call对象 + Call call = client.newCall(request); + + try { + Response response = call.execute(); + Log.v("OkHttp", "postAsync: " + response.body().string()); + //tv.setText(response.body().toString()); + }catch (IOException e){ + e.printStackTrace(); + } + } + }.start(); + } + public void getAsync(View v){ + OkHttpClient client = new OkHttpClient(); + String url = "https://www.httpbin.org/get?a=1&b=2"; + Request request = new Request.Builder() + .url(url) + .get() + .build(); + // 准备好请求的Call对象 + Call call = client.newCall(request); + + /*try { + Response response = call.execute(); + Log.v("OkHttp", "postAsync: " + response.body().string()); + //tv.setText(response.body().toString()); + }catch (IOException e){ + e.printStackTrace(); + }*/ + call.enqueue(new Callback() { + @Override + public void onFailure(@NotNull Call call, @NotNull IOException e) { + + } + + @Override + public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { + Log.v("OkHttp", "postAsync: " + response.body().string()); + } + }); + + + } + + + public void postAsync(View v){ + + new Thread(){ + @Override + public void run() { + OkHttpClient client = new OkHttpClient(); + + String url = "https://www.httpbin.org/post"; + // String url = "http://10.0.2.2:8080/index/register"; + // 表单请求体 + /*FormBody body = new FormBody.Builder() + .add("username", "zhangsan") + .add("password", "123456") + .build();*/ + + // 上传JSON对象 + MediaType JSON = MediaType.parse(" application/json; charset=utf-8"); + String jsonStr = "{\"username\":\"%赵E\", \"nickname\":\"赵 E\"}";//json数据. + RequestBody body = RequestBody.create(JSON, jsonStr); + + // 上传File文件 + /*MediaType fileType = MediaType.parse("File/*");//数据类型为File + File file = new File(" path");//file对象. + RequestBody body = RequestBody.create(fileType, file);*/ + Request request = new Request.Builder() + .url(url) + .post(body) + .build(); + + // 准备好请求的Call对象 + Call call = client.newCall(request); + + try { + Response response = call.execute(); + Log.v("OkHttp", "postAsync: " + response.body().string()); + //tv.setText(response.body().toString()); + }catch (IOException e){ + e.printStackTrace(); + } + } + }.start(); + } +} \ No newline at end of file