提交 f9fc6603 编写于 作者: 门心叼龙's avatar 门心叼龙

code perfect

上级 d7efd9fd
......@@ -26,11 +26,10 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.facebook.stetho:stetho:1.5.1'
implementation("com.squareup.okhttp3:okhttp:3.12.10")
implementation 'com.facebook.stetho:stetho:1.5.1'
implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
implementation('com.github.ihsanbal:LoggingInterceptor:3.1.0-rc5') {
implementation('com.github.ihsanbal:LoggingInterceptor:3.0.0') {
exclude group: 'org.json', module: 'json'
}
implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
......
......@@ -3,16 +3,16 @@
package="com.mxdl.okhttp3">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:networkSecurityConfig="@xml/network_security_config"
android:name=".MyApplication"
>
<activity android:name=".MainActivity">
android:usesCleartextTraffic="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"></activity>
<activity android:name=".Main2Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
......
package com.mxdl.okhttp3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.google.gson.Gson;
import com.mxdl.okhttp3.bean.User;
import java.io.IOException;
import javax.annotation.Nullable;
import javax.net.ssl.HandshakeCompletedListener;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.BufferedSink;
public class Main2Activity extends AppCompatActivity {
private TextView txtContent;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
txtContent = findViewById(R.id.txt_content);
handler = new Handler();
}
public void login(View view) {
Log.v("MYTAG", "login start...");
new Thread(new Runnable() {
@Override
public void run() {
//1.创建一个HttpClient
OkHttpClient httpClient = new OkHttpClient.Builder().build();
//2.创建一个request请求
String url = "http://192.168.0.189:8080/user/login?username=mxdl&password=123456";
Request loginRequst = new Request.Builder().url(url).build();
//3.创建一个请求命令
Call loginCall = httpClient.newCall(loginRequst);
//4.发起一个同步的请求
try {
Response loginResponse = loginCall.execute();
Log.v("MYTAG", loginResponse.body().string());
} catch (IOException e) {
Log.v("MYTAG", e.toString());
}
}
}).start();
}
public void addUser(View view) {
Log.v("MYTAG", "addUser start...");
//1.创建一个http客户端
OkHttpClient httpClient = new OkHttpClient.Builder().build();
//2.创建一个用户注册的请求
User user = new User("zhangsan", 123);
Request addUserRequest = new Request.Builder()
.url("http://192.168.0.189:8080/user/addUser")
.post(RequestBody.create(MediaType.parse("application/json;charset=utf-8"), new Gson().toJson(user)))
.build();
//3.创建一个请求命令
Call addUserCall = httpClient.newCall(addUserRequest);
//4.执行用户注册的命令
addUserCall.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.v("MYTAG", "onFail start...");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.v("MYTAG", "onResponse start...");
final String content = response.body().string();
Log.v("MYTAG", content);
//更新UI操作
handler.post(new Runnable() {
@Override
public void run() {
txtContent.setText(content);
}
});
}
});
}
}
......@@ -27,7 +27,6 @@ import com.mxdl.okhttp3.ok_chain.utils.L;
import com.mxdl.okhttp3.response.AddUserResponse;
import com.mxdl.okhttp3.response.BaseResponse;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.util.HashMap;
......@@ -165,20 +164,20 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
OkHttpClient client = new OkHttpClient();
//构建一个request请求
final Request request = new Request.Builder().url("http://192.168.31.105:8080/user/addUser")
.post(RequestBody.create("{\"userName\":\"mxdl\",\"passWord\":123456}", MediaType.parse("application/json;charset=utf-8")))
.post(RequestBody.create(MediaType.parse("application/json;charset=utf-8"),"{\"userName\":\"mxdl\",\"passWord\":123456}"))
.build();
//根据request创建一个call命令
Call call = client.newCall(request);
//执行call命令
call.enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
public void onFailure( Call call, IOException e) {
Log.v("MYTAG", "onFail start...");
Log.v("MTTAG", e.toString());
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
public void onResponse( Call call, Response response) throws IOException {
Log.v("MYTAG", "onSucc start...");
Log.v("MYTAG", response.body().string());
}
......
package com.mxdl.okhttp3.bean;
public class User {
private String userName;
private int passWord;
private String username;
private int password;
public User(String userName, int passWord) {
this.userName = userName;
this.passWord = passWord;
public User(String username, int password) {
this.username = username;
this.password = password;
}
public String getUserName() {
return userName;
public String getUsername() {
return username;
}
public void setUserName(String userName) {
this.userName = userName;
public void setUsername(String username) {
this.username = username;
}
public int getPassWord() {
return passWord;
public int getPassword() {
return password;
}
public void setPassWord(int passWord) {
this.passWord = passWord;
public void setPassword(int password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"userName='" + userName + '\'' +
", passWord=" + passWord +
"username='" + username + '\'' +
", password=" + password +
'}';
}
}
......@@ -9,13 +9,9 @@ import com.mxdl.okhttp3.ok_fly.builder.GetBuilder;
import com.mxdl.okhttp3.ok_fly.builder.PostBuilder;
import com.mxdl.okhttp3.ok_fly.call.RequestCall;
import com.mxdl.okhttp3.ok_fly.callback.RequestListener;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.util.concurrent.Executor;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
......@@ -64,12 +60,12 @@ public class FlyHttpUtil {
}
requestCall.getCall().enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
public void onFailure( Call call, IOException e) {
sendFail(e, listener);
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
public void onResponse( Call call, Response response) throws IOException {
if (call.isCanceled()) {
sendFail(new IOException("request cancel"), listener);
return;
......@@ -87,7 +83,7 @@ public class FlyHttpUtil {
});
}
private void sendFail(@NotNull final IOException e, final RequestListener listener) {
private void sendFail( final IOException e, final RequestListener listener) {
mExecutor.execute(new Runnable() {
@Override
public void run() {
......
......@@ -26,7 +26,7 @@ public class PostRequest extends BaseRequest {
@Override
public Request buildRequest() {
if(body != null ){
builder.post(RequestBody.create(new Gson().toJson(body),MediaType.parse("application/json;charset=utf-8")));
builder.post(RequestBody.create(MediaType.parse("application/json;charset=utf-8"),new Gson().toJson(body)));
}
return builder.build();
}
......
......@@ -50,7 +50,7 @@ public class OkHttpManager {
new Request
.Builder()
.url(url)
.post(RequestBody.create(jsonBody, MediaType.parse("application/json;charset=utf-8")))
.post(RequestBody.create(MediaType.parse("application/json;charset=utf-8"),jsonBody))
.build())
.enqueue(callback);
......
......@@ -8,8 +8,6 @@ import androidx.annotation.NonNull;
import com.google.gson.Gson;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
......@@ -73,7 +71,7 @@ public class MyCallBack<T> implements Callback {
}
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
public void onFailure( Call call, IOException e) {
Message message = new Message();
message.what = ON_FAIL;
message.obj = e;
......@@ -82,7 +80,7 @@ public class MyCallBack<T> implements Callback {
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) {
public void onResponse( Call call, Response response) {
try {
if (response.isSuccessful()) {
Message message = new Message();
......@@ -116,7 +114,7 @@ public class MyCallBack<T> implements Callback {
return tClass;
}
// @NotNull
//
// private Type getType() {
// Type type = mOnResponse.getClass().getGenericSuperclass();
// Type[] types = ((ParameterizedType) type).getActualTypeArguments();
......
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="同步的Get请求"
android:onClick="login"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="异步的Post请求"
android:onClick="addUser"
/>
<TextView
android:id="@+id/txt_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册