Tutorial to send ArrayListString as JSON in Adroid with AsynTask [closed]

-1

I'm looking for a tutorial or example of how to pass an ArrayList to JSON and send it through an AsynTask to a PHP server, but I do not see anything clear. Do you have any link to a good tutorial or some code already chopped around?

    
asked by wiki 27.09.2018 в 11:24
source

1 answer

1

for the first concern about how to move from a "" to Json you can use the library Gson an example of this would be:

Gson gsonTemp = new Gson();
String stringArrayList = gsonTemp.toJson(myArray);

In case you want something more manual, you can go through the arraylist string and pass it to the variable JSONArray:

JSONArray myJsonArray = new JSONArray();
for(int i = 0 ; i < myArray.size() ; i++){
myJsonArray.put(myArray.get(i));
}

for the question of sending the json in the asynctask I suggest you use HttpURLConnection

URL miurl = new URL("www.midominio.com/api/mifuncion.php");
HttpURLConnection myConnection = (HttpURLConnection) miurl.openConnection();
myConnection.setRequestMethod("POST"); // si es POST y si es GET pues pones GET
myConnection.setRequestProperty("Content-type","application/json");

DataOutputStream dataOutputStream = new DataOutputStream(myConnection.getOutputStream());
dataOutputStream.writeBytes(myJsonArray.toString());
dataOutputStream.flush();
dataOutputStream.close();

int codigoRespuesta = myConnection.getResponseCode();
Log.i("algunTAG","Server Respondio: "+codigoRespuesta);
Log.i("algunTAG","mensaje : --> "+myConnection.getResponseMessage());

greetings and good energy :) my reference is this: link

    
answered by 30.09.2018 / 01:58
source