【问题标题】:How can i send the json array to server with json?如何使用 json 将 json 数组发送到服务器?
【发布时间】:2015-11-06 16:07:52
【问题描述】:

我是 android 的初学者,我的 sqlite 数据库中有这些数据:


现在我想发送该表的一行:
我创建了这个类:

public class TourMyCountry {
        String NAME;
        String FAMILY;
        String ID;


    }


并在 android 按钮单击事件中编写以下代码:

new HttpAsyncTask().execute("http://myHOST.ir/getLIST.aspx");


并编写以下代码:

private class HttpAsyncTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
            TourMyCountry country = new TourMyCountry();

            country.ID =//READ DATA FROM SQLITE
            country.NAME=//READ DATA FROM SQLITE
            country.FAMILY=//READ DATA FROM SQLITE

            return POST(urls[0], country);
        }
@Override
        protected void onPostExecute(String result) {

        }

    }
//-----------------------
//**************************************POST LEVEL
    public static String POST(String url, TourMyCountry myCountry) {
        InputStream inputStream = null;
        String result = "";
        try {
            // 1. create HttpClient
            HttpClient httpclient = new DefaultHttpClient();
            // 2. make POST request to the given URL
            HttpPost httpPost = new HttpPost(url);
            String json = "";
            // 3. build jsonObject
            JSONObject jsonObject = new JSONObject();
            jsonObject.accumulate("id", myCountry.id);
            jsonObject.accumulate("name", myCountry.NAME);
            jsonObject.accumulate("family", myCountry.FAMILY);

            json = jsonObject.toString();
            StringEntity se = new StringEntity(json);
            json = jsonObject.toString();
            httpPost.setEntity(new StringEntity(json.toString(),"UTF-8"));
            httpPost.setHeader("Accept", "application/json");
            httpPost.setHeader("Content-type", "application/json");
            HttpResponse httpResponse = httpclient.execute(httpPost);
           inputStream = httpResponse.getEntity().getContent();
            if (inputStream != null)
                result = convertInputStreamToString(inputStream);
            else
                result = "Did not work!";

        }//end of try
        catch (Exception e) {
            Log.d("InputStream", e.getLocalizedMessage());
        }//end of catch

        // 11. return result
        return result;
    }//end of POST method
//-----------------------------
private static String convertInputStreamToString(InputStream inputStream) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String line = "";
        String result = "";
        while ((line = bufferedReader.readLine()) != null)
            result += line;

        inputStream.close();
        return result;
    }


up 代码只发送一行,效果很好,但我想发送 up 表的所有数据,我该如何解决?我可以创建 json 数组或其他方式吗?谢谢你的帮助。

【问题讨论】:

  • 创建一个JSONArrayJSONObject 是不错的选择

标签: android


【解决方案1】:

您可以使用JSONArrayJSONObject 存储在其中,然后再对其进行解析并将其发送到服务器。

代码:

JSONArray jsonArray = new JSONArray();
JSONObject obj = new JSONObject();

try {
    obj.put("id", 1)
           .put("name", "behzad")
           .put("family", razz);

} catch (JSONException e) {
     e.printStackTrace();
}
jsonArray.put(obj);

// Encodes the JSONArray as a compact JSON string
String jsonText = jsonArray.toString();

【讨论】:

  • 慢慢来。我注意到您在后端使用 ASP,因此请仔细测试您的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-06
  • 2021-03-22
  • 1970-01-01
  • 1970-01-01
  • 2013-12-27
  • 1970-01-01
相关资源
最近更新 更多