Android Studio
点击按钮后想要获得百度首页的信息并显示在TextView中,项目运行后,点击按钮,TextView没变化
package com.hjy.usingasynctask; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TextView; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class MainActivity extends AppCompatActivity { TextView text; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text = (TextView) findViewById(R.id.textView2); findViewById(R.id.read).setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { ReadURL("http://www.baidu.com"); } }); } public void ReadURL(String url){ new AsyncTask<String, Void, String>(){ @Override protected String doInBackground(String... params) {//可线程的run方法相似 try { URL url = new URL(params[0]); URLConnection connection = url.openConnection(); InputStream iStream = connection.getInputStream(); InputStreamReader isr = new InputStreamReader(iStream); BufferedReader br = new BufferedReader(isr); String line; StringBuilder builder = new StringBuilder(); while((line = br.readLine()) != null){ builder.append(line); } br.close(); iStream.close(); return builder.toString(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String result){ text.setText(result); super.onPostExecute(result); } @Override protected void onPreExecute(){ super.onPreExecute(); } @Override protected void onProgressUpdate(Void... values){ super.onProgressUpdate(values); } }.execute(url); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.hjy.usingasynctask.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="读网页信息" android:id="@+id/read" /> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Text" android:id="@+id/textView2" /> </ScrollView> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.hjy.usingasynctask"> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
还是不行。 运行没显示什么错误,就是有警告 W/System.err: Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
你把控件文本设置放在super.onPostExecute(result)下面
真的是手机不能上网的原因啊
确认手机联网了吗,确认给权限了吗