I'm beginner in android,i have this data on the my sqlite database:

now i want send the one row of that table:
i create this class:
public class TourMyCountry {
String NAME;
String FAMILY;
String ID;
}
and in android button click event write this code:
new HttpAsyncTask().execute("http://myHOST.ir/getLIST.aspx");
and write this code:
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 code just send one row,and work very good,but i want send all data of up table,how can i solve that?can i create array of json or other way?thanks for help me.
You can use the JSONArray to store JSONObjects in it before parsing it and sending it to the server.
Code:
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With