Android项目前后台交互问题,通过JSON传值
程序员文章站
2022-06-09 12:02:25
...
CrazyDao层
package com.liang.a.util;
import android.database.Cursor;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class CrazyDao {
public static ArrayList<Crazy> inputSingleJson(String json){
String[] jsonAll = json.split("\\},");
ArrayList<Crazy> list_all = new ArrayList();
for (int i=0 ; i < jsonAll.length ; i++){
Crazy crazy = new Crazy();
String[] splitJson = jsonAll[i].split(",");
crazy.setAnswer(splitJson[0].split(":")[1].split("\"")[1]);
crazy.setId(Integer.parseInt(splitJson[1].split(":")[1]));
crazy.setQuestion_img(splitJson[2].split(":")[1].split("\"")[1]);
crazy.setQuestion_type(splitJson[3].split(":")[1].split("\"")[1]);
crazy.setSelect_txt(splitJson[4].split(":")[1].split("\"")[1]);
list_all.add(crazy);
}
return list_all;
}
private static String readInputStreamToString(InputStream is) {
byte[] result;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
is.close();
baos.close();
result = baos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
return "获取数据失败";
}
return new String(result);
}
public static int getDataSize() {
int size=0;
try {
URL url = null;
url = new URL("http://10.0.2.2:8080/AndroidTest/GetAnserSize");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(5000);
conn.setRequestMethod("GET");
String resp_content = "";
int code = conn.getResponseCode();
if (conn.getResponseCode() == 200) {
InputStream is = conn.getInputStream();//从这里获取JSON数据,然后放到readInputStreamToString里面解析
size = Integer.valueOf(readInputStreamToString(is));//54
} else {
Log.e("fail", "fail");
}
} catch (Exception e) {
e.printStackTrace();
}
return size;
}
public static List<Map<String,Object>> getStageData(int nowStage){
List<Map<String,Object>> values = new ArrayList<>();
try {
ArrayList<Crazy> list = new ArrayList();
URL url = null;
url = new URL("http://10.0.2.2:8080/AndroidTest/CheckAllServlet");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(5000);
conn.setRequestMethod("GET");
String resp_content = "";
int code = conn.getResponseCode();
if (conn.getResponseCode() == 200) {
InputStream is = conn.getInputStream();
resp_content=readInputStreamToString(is);
list = inputSingleJson(resp_content);
for(int i=0;i<list.size();i++){
Map<String ,Object> map = new HashMap<>();
map.put("stage",list.get(i).getId());
if (nowStage >= list.get(i).getId()){//如果当前关不比取得的关数小
map.put("overFlag",true);//设置成true,证明玩过了的关
}else{
map.put("overFlag",false);//证明没玩儿过的关
}
values.add(map);//添加到列表中
}
} else {
Log.e("fail", "fail");
}
} catch (Exception e) {
e.printStackTrace();
}
return values;
}
public static Map<String,Object> getDataById(int id){
Map<String,Object> map = new HashMap<>();
try {
ArrayList<Crazy> list = new ArrayList();
URL url = null;
url = new URL("http://10.0.2.2:8080/AndroidTest/CheckMesById?id="+id);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(5000);
conn.setRequestMethod("GET");
String resp_content = "";
int code = conn.getResponseCode();
if (conn.getResponseCode() == 200) {
InputStream is = conn.getInputStream();
resp_content=readInputStreamToString(is);
// Log.e("content",resp_content);
JSONObject json = new JSONObject(resp_content);
map.put("answer",json.getString("answer"));
map.put("questionType",json.getString("question_type"));
map.put("questionImg",json.getString("question_img"));
map.put("selectTxt",json.getString("select_txt"));
} else {
Log.e("fail", "fail");
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
}
下一篇: 基础区间dp