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();
}
});
}