String responseBody = response.body().string();
try {
JSONObject jsonObject = new JSONObject(responseBody);
// 获取需要的字段,示例中获取msg和data.info字段
int status = jsonObject.getInt("status");
String msg = jsonObject.getString("msg");
JSONObject data = jsonObject.getJSONObject("data");
String info = data.getString("info");
// 输出结果
System.out.println("status: " + status);
System.out.println("msg: " + msg);
System.out.println("data: " + data);
System.out.println("info: " + info);
schoolDao schooldao = faceRoomDatabase.schoolDao();
schoolEntity school = new schoolEntity();
school.setInfo(info);
schooldao.insert(school);
} catch (JSONException e) {
e.printStackTrace();
}
]
记录:http请求
package com.arcsoft.arcfacedemo.dbutil;
import android.util.Log;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import android.os.Handler;
import android.os.Looper;
import org.json.JSONException;
import org.json.JSONObject;
public class GetInformation {
private static final String TAG = "GetInformation";
private static OkHttpClient okHttpClient;
private Handler handler = new Handler();
private Runnable runnable;
private Handler mHandler = new Handler(Looper.getMainLooper());
private Runnable mPollingRunnable;
private Runnable pollingRunnable;
private static FaceRoomDatabase faceRoomDatabase;
public static void asd() {
okHttpClient = new OkHttpClient();
faceRoomDatabase = FaceRoomDatabase.getInstance(ArcFaceApp.getApplication());
new Thread() {
@Override
public void run() {
try {
// 设置正确的URL
String url = "********************";
Request request = new Request.Builder()
.url(url) // 使用完整的URL
.build();
OkHttpClient okHttpClient = new OkHttpClient();
Call call = okHttpClient.newCall(request);
Response response = call.execute();
if (response.isSuccessful()) {
String responseBody = response.body().string();
toSql(responseBody);
} else {
Log.e(TAG, "Request " + response.code());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
public static void toSql(String responseBody) {
try {
JSONObject jsonObject = new JSONObject(responseBody);
// 获取需要的字段,示例中获取msg和data.info字段
int status = jsonObject.getInt("status");
String msg = jsonObject.getString("msg");
JSONObject data = jsonObject.getJSONObject("data");
String info = data.getString("info");
// 输出结果
System.out.println("status: " + status);
System.out.println("msg: " + msg);
System.out.println("data: " + data);
System.out.println("info: " + info);
} catch (JSONException e) {
e.printStackTrace();
}
}
}