0

Fiddling around with android for the first time and I can't find any solutions to my exact issue. Even though this does seem like a common problem.

When I debug my app it doesn't go into the downloadTask activity.

I have seen solutions mention asyncTask.execute() but that doesn't work.

I at a bit of a loose end. Why is my app not going into doInBackground/downloadTask?

DownloadTask (doInBackground)

public class DownloadTask extends AsyncTask<String, Void, String>{

@Override
protected String doInBackground(String... urls) {
    String result = "";
    URL url;
    HttpURLConnection urlConnection = null;

    try {
        url = new URL(urls[0]);
        urlConnection = (HttpURLConnection) url.openConnection();
        InputStream in = urlConnection.getInputStream();
        InputStreamReader reader = new InputStreamReader(in);
        int data = reader.read();
        while (data!=-1){
            char current = (char) data;
            result += current;
            data = reader.read();
        }

        return result;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);

    try {
        JSONObject jsonObject = new JSONObject(result);
        Log.d("CREATION", "GOT TO THE FIRST PART");
        JSONObject weatherDatas = new JSONObject(jsonObject.getString("main"));
        Double tempInt = Double.parseDouble(weatherDatas.getString(("temp")));
        int tempIn = (int) (tempInt*1.8-459.67);
        String placeName = jsonObject.getString("name");
        Log.d("CREATION", placeName);
        MainActivity.temperatureTextView.setText(String.valueOf(tempIn)  + "F");
        MainActivity.placeTextView.setText(placeName);

    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

MainActivity

public class MainActivity extends AppCompatActivity {

static TextView placeTextView;
static TextView temperatureTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    placeTextView = (TextView) findViewById(R.id.nameTextView);

    temperatureTextView = (TextView) findViewById(R.id.temperatureTextView);



    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    String provider = locationManager.getBestProvider(new Criteria(), false);

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        return;
    }
    Location location = locationManager.getLastKnownLocation(provider);

    Double lat = location.getLatitude();
    Double lng = location.getLongitude();
    DownloadTask task = new DownloadTask();

    task.execute("http://api.openweathermap.org/data/2.5/weather?lat=" +
            String.valueOf(lat) + "&lon=" + String.valueOf(lng) + "&appid=193a308a7dbf9e4d0d8c0b09ff296a88");

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}
JoeBoggs
  • 281
  • 1
  • 5
  • 12
  • 1
    You have called `task.execute()` `AsyncTask` should run . Do have have `INTERNET` permission in manifest ? – ADM Mar 21 '18 at 16:10
  • How you check that you don't enter `doInBackground`? If placing breakpoint, you may not see it break because you are in a different thread. – Afshin Mar 21 '18 at 16:12
  • @ADM Good point. But without INTERNET permission wouldn't an exception would beraise... – Jason Krs Mar 21 '18 at 16:13
  • Read [this](https://stackoverflow.com/questions/26890618/asynctask-doinbackground-not-called-after-calling-method-onpreexecute/26890971) – Jason Krs Mar 21 '18 at 16:13
  • Yeah i though maybe OP put breakpoint after `openConnection()`. And not in `catch` block . This is just a test case . @JoeBoggs on which API version you are testing ? – ADM Mar 21 '18 at 16:14
  • I have set INTERNET permission in manifest, @ADM API version for? – JoeBoggs Mar 21 '18 at 16:34
  • API Version for Device . Which OS ? Is it working now or not ? – ADM Mar 21 '18 at 16:35
  • @ADM Its not working yet, API v26 – JoeBoggs Mar 21 '18 at 16:38
  • @Afshin if i cant use breakpoints, how can I see if its accessed downloadTask or not? – JoeBoggs Mar 21 '18 at 16:39
  • how are you checking ? Put a `Log` on first line of `doInBackground()` and see if it prints . – ADM Mar 21 '18 at 16:40
  • @ADM Did that and it doesnt print, also added breakpoints and ran in debug but doesnt hit – JoeBoggs Mar 21 '18 at 16:41
  • @JoeBoggs you need to switch threads, if you do that breakpoint will trigger. or you can use `Log`. – Afshin Mar 21 '18 at 16:49
  • @Afshin I've used log and it didnt log anything so i dont think switching threads will give any new knowledge – JoeBoggs Mar 21 '18 at 16:51
  • @JoeBoggs You are sure that you are checking your emulator logcat? https://developer.android.com/studio/debug/index.html#systemLog because this is a mistakes that I still sometimes do. – Afshin Mar 21 '18 at 16:53
  • @Afshin yes definitely checking the emulator log – JoeBoggs Mar 21 '18 at 16:59
  • @JoeBoggs to make sure, you have allowed your app to use ACCESS_FINE_LOCATION permission,isn't it? Because if you have not, you will return at permission check and you will not create AsyncTask. – Afshin Mar 21 '18 at 17:04
  • @Afshin yes i've allowed ACCESS_FINE_LOCATION – JoeBoggs Mar 21 '18 at 17:06

1 Answers1

0

I changed your onCreate() method as following:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    placeTextView = (TextView) findViewById(R.id.nameTextView);

    temperatureTextView = (TextView) findViewById(R.id.temperatureTextView);

    DownloadTask task = new DownloadTask();

    task.execute("http://api.openweathermap.org/data/2.5/weather?lat=" +
            String.valueOf(300) + "&lon=" + String.valueOf(400) + "&appid=193a308a7dbf9e4d0d8c0b09ff296a88");

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

And I checked, it enters doInBackground() (I just copy/pasted your AsyncTask in a new project). I tested it on API level 25 though. So My guess is something is wrong with your permission requesting at runtime, because you need to request ACCESS_FINE_LOCATION at runtime.

Because you have not requested ACCESS_FINE_LOCATION at run time, check of permission fails and you return. So you will not create AsyncTask at all.

Check requesting permissions here: Requesting permissions

Afshin
  • 8,839
  • 1
  • 18
  • 53
  • I have `````` and `````` in my manifest. Is that not enough? how do i access it in runtime? – JoeBoggs Mar 21 '18 at 17:21
  • @JoeBoggs this is the problem. For INTERNET permission is enough, but for ACCESS_FINE_LOCATION, you need to request it at run time: https://developer.android.com/training/permissions/requesting.html – Afshin Mar 21 '18 at 17:22