将Json对象数据一个接一个地发送到服务器(不接受Json数组)

时间:2017-12-15 06:26:30

标签: android json

我有一个SQLite数据,应该发送到接受JSON的服务器。如果我使用JSON对象,它只发布一个数据,如果我使用JSON数组,则服务器不接受。这里的问题是我无法改变服务器端。 通过提供一些建议来帮助我。

JSON代码

private String getQuery() throws UnsupportedEncodingException,JSONException{

    Cursor a = sqLiteHelper.getData("SELECT * FROM Table");
    JSONObject jobj = new JSONObject();




        if(c.moveToFirst()) {


            do {

                jobj.put("credated_dt", a.getString(6));

                jobj.put("emp_code", a.getString(5));

                jobj.put("location", a.getString(3));

                jobj.put("name", a.getString(1));

                jobj.put("phone", a.getString(2));
                jobj.put("remarrks", a.getString(4));

            }

            while (a.moveToNext());
        }


    return jobj.toString();

}

HttpURLConnection类

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(getQuery());
 bufferedWriter.flush();
                bufferedWriter.close();
                int statusCode = httpURLConnection.getResponseCode();


                Log.d("this", " The status code is " + statusCode);

                if (statusCode == 200) {
                    inputStream = new BufferedInputStream(httpURLConnection.getInputStream());
                    String response = convertInputStreamToString(inputStream);
                    Log.d("this", "The response is " + response);
                    JSONObject jobj = new JSONObject(response);
                    return response;
                }

1 个答案:

答案 0 :(得分:0)

首先将值添加到jsonArray,然后将该数组转换为jsonObject,如下所示:

JsonObject jsonObject= new JsonObject();


 jsonObject.put("your tag",(Object)your jsonarray);

现在将此jsonobject传递给服务器...