AsyncTask HTTP HTTP Request

I want to implement a class that will handle all the HTTP requests of my application, which will be basically:

  • Get a business list (GET);
  • Login (POST);
  • Update Location (POST).

So, I will need to get the result string from the server (JSON) and pass it to other response processing methods.

I currently have the following methods:

public class Get extends AsyncTask<Void, Void, String> { @Override protected String doInBackground(Void... arg) { String linha = ""; String retorno = ""; mDialog = ProgressDialog.show(mContext, "Aguarde", "Carregando...", true); // Cria o cliente de conexão HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(mUrl); try { // Faz a solicitação HTTP HttpResponse response = client.execute(get); // Pega o status da solicitação StatusLine statusLine = response.getStatusLine(); int statusCode = statusLine.getStatusCode(); if (statusCode == 200) { // Ok // Pega o retorno BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); // Lê o buffer e coloca na variável while ((linha = rd.readLine()) != null) { retorno += linha; } } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return retorno; } @Override protected void onPostExecute(String result) { mDialog.dismiss(); } } public JSONObject getJSON(String url) throws InterruptedException, ExecutionException { // Determina a URL setUrl(url); // Executa o GET Get g = new Get(); // Retorna o jSON return createJSONObj(g.get()); } 

But g.get() returns an empty answer. How can i fix this?

+4
source share
3 answers

I think you definitely did not understand how AsyncTask works. But I believe that you want to reuse code for different tasks; if so, you can create an abstract class, and then extend it with the abstract method you created. This should be done as follows:

 public abstract class JSONTask extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... arg) { String linha = ""; String retorno = ""; String url = arg[0]; // Added this line mDialog = ProgressDialog.show(mContext, "Aguarde", "Carregando...", true); // Cria o cliente de conexão HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(mUrl); try { // Faz a solicitação HTTP HttpResponse response = client.execute(get); // Pega o status da solicitação StatusLine statusLine = response.getStatusLine(); int statusCode = statusLine.getStatusCode(); if (statusCode == 200) { // Ok // Pega o retorno BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); // Lê o buffer e coloca na variável while ((linha = rd.readLine()) != null) { retorno += linha; } } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return retorno; // This value will be returned to your onPostExecute(result) method } @Override protected void onPostExecute(String result) { // Create here your JSONObject... JSONObject json = createJSONObj(result); customMethod(json); // And then use the json object inside this method mDialog.dismiss(); } // You'll have to override this method on your other tasks that extend from this one and use your JSONObject as needed public abstract customMethod(JSONObject json); } 

And then your activity code should look something like this:

 YourClassExtendingJSONTask task = new YourClassExtendingJSONTask(); task.execute(url); 
+12
source

You are not completing a task. You just create it. I think you need to do:

 Get g = new Get(); g.execute(); 

But you are using the task life cycle incorrectly. OnPostExecute runs in the main thread, where you need to do all the updates as needed. For example, you can pass a View task.

+1
source

It looks like you never run AsyncTask by calling the execute () function on the Get object.

try this code:

 Get g = new Get(); g.execute(); 
+1
source

Source: https://habr.com/ru/post/1390557/


All Articles