我想将带有json的ArrayList<Orto> listOrto
发送到我的服务器。这是我的代码
String url = serverUrl + "addOrto1212"; //tırnak içi addOrto olacak
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 100000);
try{
JSONObject json = new JSONObject();
Gson gson = new GsonBuilder().create();
JsonArray myCustomArray = gson.toJsonTree(listOrto).getAsJsonArray();
json.put("list",myCustomArray);
Log.d("******************json",json.toString());
HttpPost post = new HttpPost(url);
StringEntity se = new StringEntity(json.toString());
post.setEntity(se);
client.execute(post);
}catch(Exception e){
//
}
让我们看看Logcat;
03-30 09:24:18.655 21698-22331/com.emato.microorthodontist D/******************json: {"macid":"C9:3F:C1:B1:6F:1F","list":"[{\"date\":\"2016\/03\/30\",\"mac\":\"C9:3F:C1:B1:6F:1F\",\"id\":31,\"dest\":0,\"melvalue\":11},{\"date\":\"2016\/03\/29\",\"mac\":\"C9:3F:C1:B1:6F:1F\",\"id\":16,\"dest\":0,\"melvalue\":95},{\"date\":\"2016\/03\/28\",\"mac\":\"C9:3F:C1:B1:6F:1F\",\"id\":17,\"dest\":0,\"melvalue\":0},{\"date\":\"2016\/03\/27\",\"mac\":\"C9:3F:C1:B1:6F:1F\",\"id\":18,\"dest\":0,\"melvalue\":0},{\"date\":\"2016\/03\/26\",\"mac\":\"C9:3F:C1:B1:6F:1F\",\"id\":19,\"dest\":0,\"melvalue\":0},{\"date\":\"2016\/03\/25\",\"mac\":\"C9:3F:C1:B1:6F:1F\",\"id\":20,\"dest\":0,\"melvalue\":0},{\"date\":\"2016\/03\/24\",\"mac\":\"C9:3F:C1:B1:6F:1F\",\"id\":21,\"dest\":0,\"melvalue\":0},{\"date\":\"2016\/03\/23\",\"mac\":\"C9:3F:C1:B1:6F:1F\",\"id\":22,\"dest\":0,\"melvalue\":0},{\"date\":\"2016\/03\/22\",\"mac\":\"C9:3F:C1:B1:6F:1F\",\"id\":23,\"dest\":0,\"melvalue\":0},{\"date\":\"2016\/03\/21\",\"mac\":\"C9:3F:C1:B1:6F:1F\",\"id\":24,\"dest\":0,\"melvalue\":0},{\"date\":\"2016\/03\/20\",\"mac\":\"C9:3F:C1:B1:6F:1F\",\"id\":25,\"dest\":0,\"melvalue\":0},{\"date\":\"2016\/03\/19\",\"mac\":\"C9:3F:C1:B1:6F:1F\",\"id\":26,\"dest\":0,\"melvalue\":0},{\"date\":\"2016\/03\/18\",\"mac\":\"C9:3F:C1:B1:6F:1F\",\"id\":27,\"dest\":0,\"melvalue\":0},{\"date\":\"2016\/03\/17\",\"mac\":\"C9:3F:C1:B1:6F:1F\",\"id\":28,\"dest\":0,\"melvalue\":0},{\"date\":\"2016\/03\/16\",\"mac\":\"C9:3F:C1:B1:6F:1F\",\"id\":29,\"dest\":0,\"melvalue\":0},{\"date\":\"2016\/03\/15\",\"mac\":\"C9:3F:C1:B1:6F:1F\",\"id\":30,\"dest\":0,\"melvalue\":0}]"}
&#34;列表&#34;我的listorto返回一个字符串,但我想要jsonArray。我的错误在哪里请帮助..
答案 0 :(得分:0)
您可以创建POJO类来表示您正在使用的数据,例如:
public class MyData {
private String macid;
private ArrayList<MyListItem> myList= new ArrayList<MyListItem>();
// create setters and getters
}
public class MyListItem {
private String date;
private String mac;
private Integer id;
private Integer dest;
private Integer melvalue;
// create setters and getters
}
然后使用GSON转换为JSON:
填充数据对象......
MyData mydata = new MyData();
mydata.setMacid("some:mac:id");
MyListItem item = new MyListItem();
//item .set ... set other values for the item
mydata.getMyList().add(item);//add as many items as you want
创建Gson实例:
Gson gson = new Gson();
1-到Json:
//from object to json string
String mydataAsJson = gson.toJson(mydata, MyData.class);
2-来自Json:
//from json string to object
String jsonStr = getJsonFromServer();
MyData mydata2 = gson.fromJson(jsonStr, MyData.class);