6.14

package com.example.my2mysql.tool;


import androidx.annotation.NonNull;

import com.example.my2mysql.Pojo.Plan;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class okhttp3manager { //这是一个网络请求工具类 我们自定义
private static final String url = "http://10.99.115.93:8080";
public interface LoginCallback{ //登录回调接口

void LoginSuccess(int auth); //回调返回一个int 类型的 auth 表明登录者的身份
void LoginFailure(String errorMessage); //登录失败的错误信息


}
public interface RegisterCallback{
void RegisterSuccess(String successMessage);
void RegisterFailure(String errorMessage);

}
public interface DataListCallback{
void onPlanReceived(Plan plan);

void onPlanFailure(String errorMessage);
}

public static void login(String userId ,String password,final LoginCallback callback){
String loginUrl = url+"/login"; //url地址
OkHttpClient okHttp = new OkHttpClient();
JSONObject json = new JSONObject();
try {
json.put("userId", userId); //将id 放入json
json.put("password", password);//将password放入json
} catch (JSONException e) {
throw new RuntimeException(e);
}
//创建一个请求体,将JSONObject转换为字符串并指定为JSON格式。
RequestBody requestBody = RequestBody.create(json.toString(), okhttp3.MediaType.parse("application/json; charset=utf-8"));

Request request = new Request.Builder() //新建一个请求创建一个新的请求,指定URL和请求体为POST方法。
.url(loginUrl)
.post(requestBody)
.build();
okHttp.newCall(request).enqueue(new Callback() {
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
if (response.isSuccessful()) {
String responseData = response.body().string();
try {
JSONObject jsonResponse = new JSONObject(responseData);
if (jsonResponse.has("error")) {
final String errorMessage = jsonResponse.getString("error");
callback.LoginFailure(errorMessage);
} else if (jsonResponse.has("success")) {
int auth = jsonResponse.getInt("auth");
callback.LoginSuccess(auth);

}
} catch (JSONException e) {
e.printStackTrace();
}
}
}

@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
callback.LoginFailure("网络错误");
}

});
}

public static void register(String id,String name,String password,String sclass, String phone,RegisterCallback callback){
String registerUrl = url+"/add";
OkHttpClient client=new OkHttpClient();
JSONObject json=new JSONObject();
try{json.put("id",id);
json.put("name",name);
json.put("password",password);
json.put("sclass",sclass);
json.put("phone",phone);

} catch (JSONException e){
throw new RuntimeException(e);
}
RequestBody requestBody=RequestBody.create(json.toString(),okhttp3.MediaType.parse("application/json; charset=utf-8"));
Request request = new Request.Builder() //新建一个请求创建一个新的请求,指定URL和请求体为POST方法。
.url(registerUrl)
.post(requestBody)
.build();

client.newCall(request).enqueue(new Callback() {
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
if (response.isSuccessful()) {
String responseData = response.body().string();
try {
JSONObject jsonResponse = new JSONObject(responseData);
if (jsonResponse.has("error")) {
final String errorMessage = jsonResponse.getString("error");
callback.RegisterFailure(errorMessage);
} else if (jsonResponse.has("success")) {
String successMessage = jsonResponse.getString("success");
callback.RegisterSuccess(successMessage);

}
} catch (JSONException e) {
e.printStackTrace();
}
}
}

@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
callback.RegisterFailure("网络错误");
}

});
}
public void getPlanDataFromBackend(String id, DataListCallback callback) {
String planUrl = url + "/getPlan?id=" + id;
OkHttpClient client = new OkHttpClient();

Request request = new Request.Builder()
.url(planUrl)
.get()
.build();

client.newCall(request).enqueue(new Callback() {
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
if (response.isSuccessful()) {
String responseData = response.body().string();
try {
JSONObject jsonResponse = new JSONObject(responseData);
if (jsonResponse.has("plan")) {
JSONObject jsonPlan = jsonResponse.getJSONObject("plan");
Plan plan = new Plan();
plan.setWorkplan(jsonPlan.getString("workplan"));
plan.setStartdate(jsonPlan.getString("startdate"));
plan.setEnddate(jsonPlan.getString("enddate"));
plan.setIsdone(jsonPlan.getString("isdone"));

callback.onPlanReceived(plan);
} else {
callback.onPlanFailure("未找到计划数据");
}
} catch (JSONException e) {
e.printStackTrace();
callback.onPlanFailure("数据解析错误");
}
}
}

@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
callback.onPlanFailure("网络错误");
}
});
}



}

package com.example.my2mysql.tool;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.my2mysql.Pojo.Plan;
import com.example.my2mysql.R;

import java.util.List;

public class MyAdapter extends ArrayAdapter<Plan> {
private Context context;

public MyAdapter(Context context, List<Plan> data) {
super(context, 0, data);
this.context = context;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}

Plan currentPlan = getItem(position);

TextView dataid = listItemView.findViewById(R.id.dataid);
TextView datastarttime = listItemView.findViewById(R.id.datastarttime);
TextView dataendtime = listItemView.findViewById(R.id.dataendtime);
TextView dataisdone = listItemView.findViewById(R.id.dataisdone);

if (currentPlan != null) {
dataid.setText(String.valueOf(currentPlan.getId()));
datastarttime.setText(currentPlan.getStartdate());
dataendtime.setText(currentPlan.getEnddate());
dataisdone.setText(currentPlan.getIsdone());
}

return listItemView;
}
}
posted @ 2024-06-18 09:24  晨观夕  阅读(1)  评论(0编辑  收藏  举报