【发布时间】:2018-03-21 16:08:04
【问题描述】:
第一次摆弄 android,我找不到任何解决我的确切问题的方法。尽管这看起来确实是一个常见问题。
当我调试我的应用程序时,它不会进入 downloadTask 活动。
我看到解决方案提到了asyncTask.execute(),但这不起作用。
我有点不知所措。为什么我的应用没有进入doInBackground/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();
}
});
}
【问题讨论】:
-
你调用了
task.execute()AsyncTask应该运行。清单中有INTERNET权限吗? -
你如何检查你没有输入
doInBackground?如果放置断点,您可能看不到它中断,因为您在不同的线程中。 -
@ADM 好点。但如果没有 INTERNET 许可,就不会出现异常......
-
阅读this
-
是的,我可能会在
openConnection()之后放置断点。而不是在catch块中。这只是一个测试用例。 @JoeBoggs 您正在测试哪个 API 版本?
标签: android