Java Android中使用httpurlconnection的FatalShuttdown
事实上,我正在使用HttpUrlConnection修改我的应用程序,我尝试了互联网上最简单的代码,但仍然在logcat中遇到了一个致命的问题,我不理解这个问题 代码如下:Java Android中使用httpurlconnection的FatalShuttdown,java,android,httpurlconnection,Java,Android,Httpurlconnection,事实上,我正在使用HttpUrlConnection修改我的应用程序,我尝试了互联网上最简单的代码,但仍然在logcat中遇到了一个致命的问题,我不理解这个问题 代码如下: try { URL url = new URL("http://www.android.com/"); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); InputStream in = new
try {
URL url = new URL("http://www.android.com/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader r = new BufferedReader(new InputStreamReader(in));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line).append('\n');
}
response_textview.setText(total.toString());
urlConnection.disconnect();
}
catch (MalformedURLException e){
response_textview.setText(e.getMessage());
}
catch (IOException e){
response_textview.setText(e.getMessage());
}
我在日志里看到了这个:
谢谢您需要在新线程中提出请求。可能,您的代码(它只是try-catch)正在主线程上运行。此操作被拒绝。尝试使用例如Thread.class在新线程中发出请求。您需要在新线程中发出请求。可能,您的代码(它只是try-catch)正在主线程上运行。此操作被拒绝。尝试使用例如Thread.class在新线程中发出请求。使用
doInBackground()
方法包装代码。使用doInBackground()
方法包装代码。您不应该在主线程上执行网络操作。使用异步任务
1.创建AsyncTask对象并将代码移动到doInBackground方法中,如下所述:
AsyncTask task = new AsyncTask<Void,String,String>(){
@Override
protected String doInBackground(Void... params) {
// perform your network operation here and return response
try {
URL url = new URL("http://www.android.com/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader r = new BufferedReader(new InputStreamReader(in));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line).append('\n');
}
urlConnection.disconnect();
}
catch (MalformedURLException e){
return e.getMessage();
}
catch (IOException e){
return e.getMessage();
}
return total.toString();
}
protected void onPostExecute(String response){
// response returned by doInBackGround() will be received
// by onPostExecute(String response)
response_textview.setText(response);
}
};
您不应该在主线程上执行网络操作。使用异步任务 1.创建AsyncTask对象并将代码移动到doInBackground方法中,如下所述:
AsyncTask task = new AsyncTask<Void,String,String>(){
@Override
protected String doInBackground(Void... params) {
// perform your network operation here and return response
try {
URL url = new URL("http://www.android.com/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader r = new BufferedReader(new InputStreamReader(in));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line).append('\n');
}
urlConnection.disconnect();
}
catch (MalformedURLException e){
return e.getMessage();
}
catch (IOException e){
return e.getMessage();
}
return total.toString();
}
protected void onPostExecute(String response){
// response returned by doInBackGround() will be received
// by onPostExecute(String response)
response_textview.setText(response);
}
};
这个“最简单代码”的可能副本是要在纯Java中运行,而不是作为Android应用程序运行。请参阅Android网络文档。这个“最简单代码”的可能副本是要在纯Java中运行,而不是作为Android应用程序运行。请参阅Android网络文档Nice try to get memory Leak Nice try to get memory Leak如何在线程中设置文本视图值?response_textview.setText(如getMessage());您不能在后台更新UI。这只是一个示例,但您仍然可以使用runOnUIThread。这和将文本视图传递给异步任务而不使用WeakReference包装文本视图一样糟糕。理想的方法是使用RxJava。无需将文本视图传递给异步任务。异步任务对象是在同一个类中创建的,可以直接访问文本视图。您想如何更新
catch
中的response\u textview
?很好,这就是复制粘贴问题。我复制了实际的问题,并把它放到了doInBackground中。无论如何,它现在已经更新了。ThanksHow可以在线程中设置文本视图值吗?response_textview.setText(如getMessage());您不能在后台更新UI。这只是一个示例,但您仍然可以使用runOnUIThread。这和将文本视图传递给异步任务而不使用WeakReference包装文本视图一样糟糕。理想的方法是使用RxJava。无需将文本视图传递给异步任务。异步任务对象是在同一个类中创建的,可以直接访问文本视图。您想如何更新catch
中的response\u textview
?很好,这就是复制粘贴问题。我复制了实际的问题,并把它放到了doInBackground中。无论如何,它现在已经更新了。谢谢