首页 > 其他分享 >6.14

6.14

时间:2024-06-18 09:49:04浏览次数:9  
标签:String 6.14 json new import public NonNull

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;
}
}

标签:String,6.14,json,new,import,public,NonNull
From: https://www.cnblogs.com/wcy1111/p/18253707

相关文章

  • JSON响应中提取特定的信息——6.14山大软院项目实训2
    在收到的JSON响应中提取特定的信息(如response字段中的文本)并进行输出,需要进行JSON解析。在Unity中,可以使用JsonUtility进行简单的解析,但由于JsonUtility对嵌套对象的支持有限,通常推荐使用第三方库如Newtonsoft.Json来处理复杂的JSON结构。首先,确保Unity项目中已经包含了Newton......
  • 简单处理字符串——6.14山大软院项目实训1
    对于直接输出服务器返回的json到Debug,发现他还包含json的结构,但是不想调试json的返回结构,可以使用简单地处理字符串的方法,而不引入额外的库或复杂的JSON解析,但是这个解决方式是暂时的是投机取巧的,正确的做法我会在下一条博客里面写出来。可以考虑使用字符串操作方法直接从接收......
  • 6.14
    实验一数据库和表的建立、数据操作一、实验目的:掌握使用SQL语言进行数据定义和数据操纵的方法。二、实验要求:建立一个数据库stumanage,建立三个关系表student,course,sc。向表中插入数据,然后对数据进行删除、修改等操作,对关系、数据库进行删除操作。三、实验步骤:1、开始......
  • 6.14
    复习计网以及数据库,完成了web的实验四<html><head><title>学生管理系统</title><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><linkrel="stylesheet"type="text/css"href="css/s......
  • 6.14博客
    周五了太棒了学习内容:安卓packagecom.example.app_02;importorg.junit.Rule;importorg.junit.Test;importstaticorg.junit.Assert.*;importcom.example.app_02.entity.Record;importcom.example.app_02.utils.RecordDao;importjava.util.ArrayList;/** *......
  • 6.14
    今日学习总结学习时间1,5hpackagecom.app.chapter04;importandroid.content.Intent;importandroid.os.Bundle;importandroid.view.View;importandroidx.activity.EdgeToEdge;importandroidx.appcompat.app.AppCompatActivity;importandroidx.core.graphics.Insets;import......
  • 6.14实验四:共轭梯度法程序设计
    实验四:共轭梯度法程序设计一、实验目的掌握共轭梯度法的基本思想及其迭代步骤;学会运用MATLAB编程实现常用优化算法;能够正确处理实验数据和分析实验结果及调试程序。  二、实验内容(1)求解无约束优化问题:(2)终止准则取;(3)完成FR共轭梯度法的MATLAB编程、调试;(4)选取几个与实验二......
  • 6.14安卓开发日记58
     实验三:Newton法程序设计一、实验目的掌握Hesse矩阵的计算方法和Newton法的基本思想及其迭代步骤;学会运用MATLAB编程实现常用优化算法;能够正确处理实验数据和分析实验结果及调试程序。二、实验内容(1)求解无约束优化问题:;(2)终止准则取;(3)完成Newton法(牛顿法)的MATLAB编程、调试......
  • 6.14-二叉树遍历
    题目分类题目分类大纲如下:二叉树的种类在我们解题过程中二叉树有两种主要的形式:满二叉树和完全二叉树。满二叉树满二叉树:如果一棵二叉树只有度为0的结点和度为2的结点,并且度为0的结点在同一层上,则这棵二叉树为满二叉树。如图所示:这棵二叉树为满二叉树,也可以说深度为k,有......
  • 6.14 哈希表
    采用邻接表创建无向图G,依次输出各顶点的度。输入格式:输入第一行中给出2个整数i(0<i≤10),j(j≥0),分别为图G的顶点数和边数。输入第二行为顶点的信息,每个顶点只能用一个字符表示。依次输入j行,每行输入一条边依附的顶点。输出格式:依次输出各顶点的度,行末没有最后的空格。输入......