第一次摆弄Android,我无法找到解决我问题的方法。尽管这似乎是一个常见的问题。
当我调试我的应用程序时,它不会进入downloadTask活动。
我看过提及asyncTask.execute()
的解决方案,但这不起作用。
我有点松散的结局。为什么我的应用不会进入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();
}
});
}
答案 0 :(得分:0)
我更改了您的onCreate()
方法,如下所示:
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();
}
});
}
我检查过,输入 doInBackground()
(我只是在新项目中复制/粘贴了您的AsyncTask
)。我在API级别25测试了它。所以我的猜测是你的权限在运行时请求有问题,因为你需要在运行时请求ACCESS_FINE_LOCATION。
由于您未在运行时请求ACCESS_FINE_LOCATION,因此检查权限失败并返回。因此,您根本不会创建AsyncTask
。
在此处检查请求权限:Requesting permissions