2

I am trying to make a function that returns a json (using org.json dependencies) from a http request. To do this in Android Java, it is necessary to create a AsyncTask

public class MainActivity extends AppCompatActivity {

TextView resultQuery;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    resultQuery = findViewById(R.id.resultQuery);
    try {
        new sendUrl().execute();

    } catch (Exception e) {
        e.printStackTrace();
        resultQuery.setText("Erro: " + e);
        Toast.makeText(this, "Erro" + e, Toast.LENGTH_SHORT).show();
    }
}

private class sendUrl extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params){

        try {
            call_me();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    @Override
    protected void onPostExecute(String message){
        Toast.makeText(MainActivity.this, "PostExecute", Toast.LENGTH_SHORT).show();
    }
}

public void call_me() throws Exception {
    String url = "https://api.github.com/users/leonanml";
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    // optional default is GET
    con.setRequestMethod("GET");
    //add request header
    con.setRequestProperty("User-Agent", "Mozilla/5.0");
    int responseCode = con.getResponseCode();
    //System.out.println("\nSending 'GET' request to URL : " + url);
    Toast.makeText(this, "Response Code : " + responseCode, Toast.LENGTH_SHORT).show();
    BufferedReader in = new BufferedReader(
            new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();
    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();
    //print in String
    //System.out.println(response.toString());
    //Read JSON response and print
    JSONObject myResponse = new JSONObject(response.toString());
    //System.out.println("result after Reading JSON Response");
    //System.out.println("id: "+myResponse.getString("login"));
    resultQuery.setText(response.toString());
}}

Currently my "sendUrl" class is giving a warning :

This AsyncTask class should be static or leaks might occur (com.muller.httprequest.MainActivity.sendUrl) Inspection info:A static field will leak contexts.

Syed Ahmed Jamil
  • 1,881
  • 3
  • 18
  • 34
  • 2
    Possible duplicate of [Warning: This AsyncTask class should be static or leaks might occur](https://stackoverflow.com/questions/44309241/warning-this-asynctask-class-should-be-static-or-leaks-might-occur) – second Sep 11 '19 at 00:33

1 Answers1

0

When you use an inner class that is not static, it can't be collected by the garbage collection so it's gonna keep using memory, easy fix is to just make the inner class static or make it out of the activity class.

DarkCeptor44
  • 184
  • 3
  • 15