我正在尝试将数据从sqlite发送到服务器作为json对象。它正在捕获数据,但只有一个数据(行)进入服务器。
我的json代码是
JSONObject jobj = new JSONObject();
if (c.moveToFirst()) {
do {
jobj.put("add_fld1", "Test1");
jobj.put("add_fld2", "Test2");
jobj.put("add_fld3", "Test3");
jobj.put("credated_dt", c.getString(6));
// jobj.put("id", c.getInt(0));
jobj.put("emp_code", c.getString(5));
jobj.put("location", c.getString(3));
jobj.put("name", c.getString(1));
jobj.put("phone", c.getString(2));
jobj.put("remarrks", c.getString(4));
}
while (c.moveToNext());
}
return jobj.toString();
}
JsonArray正在运行,但服务器现在没有显示数据。
JsonArray代码
private String getQuery() throws UnsupportedEncodingException, JSONException {
Cursor c = sqLiteHelper.getData("SELECT * FROM DETAILS");
JSONObject jobj = new JSONObject();
JSONArray jsonArray = new JSONArray();
while (c.moveToNext()) {
jobj.put("add_fld1", "Test1");
jobj.put("add_fld2", "Test2");
jobj.put("add_fld3", "Test3");
jobj.put("credated_dt", c.getString(6));
// jobj.put("id", c.getInt(0));
jobj.put("emp_code", c.getString(5));
jobj.put("location", c.getString(3));
jobj.put("name", c.getString(1));
jobj.put("phone", c.getString(2));
jobj.put("remarrks", c.getString(4));
jsonArray.put(jobj);
}
return jsonArray.toString();
}
Json Array正在捕获所有数据并且响应成功但服务器没有显示JsonArray数据,但它显示了jsonobject数据。
答案 0 :(得分:0)
您应该制作一个arraylist并将所有数据放入其中并将数据发送到服务器
JSONObject jobj = new JSONObject();
ArrayList<JSONObject> arrayList=new ArrayList();
if (c.moveToFirst()) {
do {
jobj.put("add_fld1", "Test1");
jobj.put("add_fld2", "Test2");
jobj.put("add_fld3", "Test3");
jobj.put("credated_dt", c.getString(6));
// jobj.put("id", c.getInt(0));
jobj.put("emp_code", c.getString(5));
jobj.put("location", c.getString(3));
jobj.put("name", c.getString(1));
jobj.put("phone", c.getString(2));
jobj.put("remarrks", c.getString(4));
arrayList.add(jobj);
}
while (c.moveToNext());
}
return arrayList;
}
现在你可以从这个arraylist获得所有记录。 它帮帮我,请检查一下。
答案 1 :(得分:0)
您应该创建一个JSONArray并将所有数据放入其中并将数据发送到服务器
JSONArray jsonArray = new JSONArray();
if (c.moveToFirst()) {
do {
JSONObject jobj= new JSONObject();
jobj.put("add_fld1", "Test1");
jobj.put("add_fld2", "Test2");
jobj.put("add_fld3", "Test3");
jobj.put("credated_dt", c.getString(6));
// jobj.put("id", c.getInt(0));
jobj.put("emp_code", c.getString(5));
jobj.put("location", c.getString(3));
jobj.put("name", c.getString(1));
jobj.put("phone", c.getString(2));
jobj.put("remarrks", c.getString(4));
jsonArray.put(jobj);
}
while (c.moveToNext());
}
return jsonArray.toString();
}
答案 2 :(得分:0)
<强>解决方案:强>
再次将JSONArray转换为JSONObject并逐个发送它解决了我的问题
protected String doInBackground(String... params) {
boolean flag = false;
try {
URL url = new URL();
JSONArray ja = getQuery();
for (int i = 0; i < ja.length(); i++) {
JSONObject jj = ja.getJSONObject(i);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestProperty("Accept", "application/json");
httpURLConnection.setRequestProperty("Content-type", "application/json");
httpURLConnection.setRequestMethod("POST");
outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write(jj.toString());
Log.e("posted", "" + jj);
Log.e("doInBackground", "postDataParams");
bufferedWriter.flush();
bufferedWriter.close();
int statusCode = httpURLConnection.getResponseCode();
Log.d("this", " The status code is " + statusCode);
if (statusCode == 200) {
inputStream = new BufferedInputStream(httpURLConnection.getInputStream());
flag =true;
}
inputStream.close();
outputStream.close();
httpURLConnection.disconnect();
}
} catch (Exception e) {
Log.e("streams1", "The response is " + e.getLocalizedMessage());
} finally {
Log.e("Delete", "Going to delete");
sqLiteHelper.deleteAll();
}
if(flag)
return "success";
return null;
}