gpt4 book ai didi

android - 如何使用 json 将 json 数组发送到服务器?

转载 作者:太空狗 更新时间:2023-10-29 14:04:21 25 4
gpt4 key购买 nike

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


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

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 数组或其他方式吗?谢谢帮助。

最佳答案

您可以使用 JSONArray在解析它并将其发送到服务器之前将 JSONObject 存储在其中。

代码:

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();

关于android - 如何使用 json 将 json 数组发送到服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33570909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com