0
I rephrased the question again to try to clarify better the need of my situation:
I need to run between asa Activity s the following operation, when clicking the button, the application sends information to a php page, which from that information generates a Query, validates the data and creates a JSON file on the server, then the application takes this file and reads the JSON file.
Currently, I can send the information to a PHP that generates JSON and I can also read the JSON file, but I cannot run both functions in sequence in the application. Someone has a view of how to run this process with Asynctask or other native Android class.
Below the code I retrieve the information in the JSON file that is saved on the server.
public class BackGroundWorkerItensActivity extends AsyncTask<String, Void, String> {
Context context;
public BackGroundWorkerItensActivity(Context context){
this.context = context;
}
@Override
protected String doInBackground(String... params) {
String iddist = params[0];
String url_receber = "http://minhaurl.com/teste/dados.json";
try {
URL url = new URL(url_receber);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String result="";
String line = "";
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (IOException e) {
Log.i("DadosDist", "Erro na lista dos itens!");
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Intent intentLD = new Intent(context, MainActivity.class);
Log.i("Vem JSON", s);
intentLD.putExtra("JSON", s);
context.startActivity(intentLD);
}
In case it is not well detailed, let me know that I will add more information.
You want to generate a JSON with the data sent by the client (from Android)? '-'
– Valdeir Psr
No, I edited my question to specify better
– Gabriel Henrique
It’s not clear yet.
– Piovezan
It’s still very broad. Is this data generated in a database or would you extract it from a website? What you have tried to do (code interface) to generate this data?
– Valdeir Psr
For example, I send the code "1" to a php page, which will generate the JSON file, after generating the JSON file, I will read this file by the application and present the file data to the user. In case there is an easier way to send/receive data between an application and webservice, we welcome the answer..
– Gabriel Henrique
Gabriel take a look here, much easier and you don’t even have to do the async https://github.com/koush/ion#get-json
– AndersonCanteiro
would not just pass Query to webservice?
– Igor Oliveira
No, I need to pass only one parameter to my PHP that already does the Query and receive a JSON back from PHP to load into the App.
– Gabriel Henrique