我对Android应用程序有疑问。我试图在教程的帮助下创建一个基本的应用程序来连接同一台服务器。但是运行代码后,我收到了错误:android.os.NetworkOnMainThreadException。
这是我的代码:
package com.czerwinski.top10downloader;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: starting async task");
DownloadData downloadData = new DownloadData();
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.INTERNET) == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "onCreate: Internet Permission Given");
downloadData.doInBackground("http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topfreeapplications/limit=10/xml");
}
Log.d(TAG, "onCreate: done...");
}
private class DownloadData extends AsyncTask<String, Void, String> {
private static final String TAG = "DownloadData";
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.d(TAG, "onPostExecute: parameter is: " + s);
}
@Override
protected String doInBackground(String... strings) {
Log.d(TAG, "doInBackground: starts with: " + strings[0]);
String rssFeed = downloadXML(strings[0]);
if(rssFeed == null) {
Log.i(TAG, "doInBackground: Error downloading");
}
return rssFeed;
}
private String downloadXML(String urlPath) {
Log.d(TAG, "downloadXML: Starting downloadXML");
StringBuilder xmlResult = new StringBuilder();
try {
URL url = new URL(urlPath);
Log.d(TAG, "downloadXML: Created URL");
URLConnection connection = (URLConnection)url.openConnection();
connection.setConnectTimeout(10 * 1000);
connection.setReadTimeout(10 * 1000);
HttpURLConnection httpConnection = (HttpURLConnection) connection;
Log.d(TAG, "downloadXML: Opened connection");
int response = httpConnection.getResponseCode();
Log.d(TAG, "downloadXML: The response code was " + response);
// InputStream inputStream = connection.getInputStream();
// InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
// BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
int charsRead;
char[] inputBuffer = new char[500];
while(true) {
charsRead = bufferedReader.read(inputBuffer);
if(charsRead < 0) {
break;
}
if(charsRead > 0) {
xmlResult.append(String.copyValueOf(inputBuffer, 0, charsRead));
}
}
bufferedReader.close();
Log.d(TAG, "downloadXML: The response was: " + xmlResult.toString());
return xmlResult.toString();
} catch (MalformedURLException e) {
Log.e(TAG, "downloadXML: Invalid URL" + e.getMessage());
} catch (IOException e) {
Log.e(TAG, "downloadXML: IO Exception reading data " + e.getMessage());
} catch (Exception e) {
Log.e(TAG, "downloadXML: General exception: " + e);
}
return null;
}
}
}
这是我收到的一些错误输出:
2018-10-05 18:41:46.225 31414-31414 / com.czerwinski.top10downloader D / DownloadData:doInBackground:始于:http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topfreeapplications/limit=10/xml
2018-10-05 18:41:46.225 31414-31414 / com.czerwinski.top10downloader D / DownloadData:downloadXML:开始downloadXML
2018-10-05 18:41:46.225 31414-31414 / com.czerwinski.top10downloader D / DownloadData:downloadXML:创建的URL
2018-10-05 18:41:46.229 31414-31414 / com.czerwinski.top10downloader D / Download数据:downloadXML:已打开连接
2018-10-05 18:41:46.231 31414-31414 / com.czerwinski.top10downloader E / DownloadData:downloadXML:一般异常:android.os.NetworkOnMainThreadException
2018-10-05 18:41:46.231 31414-31414 / com.czerwinski.top10downloader I / DownloadData:doInBackground:错误下载
有人知道我的代码有什么问题吗?
感谢您将提供的任何帮助:)
更新1
我相信我已经完成了此问题中提到的步骤: How do I fix android.os.NetworkOnMainThreadException?
负责连接到网络的方法在扩展AsyncTask的类中,该类在 doInBackground 方法中调用。尽管如此,每次我午餐调试时都会出现该错误。我仍在寻找问题的答案。