Java 在TextView上不获取任何输出
我制作了一个小应用程序,返回一个网站的html供练习。但是我没有得到文本视图的输出。代码如下:Java 在TextView上不获取任何输出,java,android,android-edittext,textview,Java,Android,Android Edittext,Textview,我制作了一个小应用程序,返回一个网站的html供练习。但是我没有得到文本视图的输出。代码如下: public class MainActivity extends Activity { TextView httpresponse; EditText WebAddress; String website; Button getHtml; HttpClient client; @Override protected void onCreate(Bundle savedInstanceState)
public class MainActivity extends Activity {
TextView httpresponse;
EditText WebAddress;
String website;
Button getHtml;
HttpClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebAddress = (EditText) findViewById(R.id.editText1);
//client = new DefaultHttpClient();
httpresponse = (TextView) findViewById(R.id.tvHttpStuff);
getHtml = (Button) findViewById(R.id.getHtmlBtn);
getHtml.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
website = WebAddress.getText().toString();
//httpresponse.setText(website);
//GetThatHttp http = new GetThatHttp();
String returned;
try{
returned = getInternetData();
httpresponse.setText(returned);
}catch(Exception e){
e.printStackTrace();
}
}
});
}
public String getInternetData() throws Exception{
BufferedReader buffReader=null;
String data =null;
try{
HttpClient client = new DefaultHttpClient();
URI websiteURL = new URI(website);
HttpGet request = new HttpGet();
request.setURI(websiteURL);
HttpResponse response = client.execute(request);
buffReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer stringBuilder = new StringBuffer("");
String line = " ";
String newLine = System.getProperty("line.separator");
while((line = buffReader.readLine())!= null){
stringBuilder.append(line + newLine);
}
buffReader.close();
data = stringBuilder.toString();
return data;
}finally{
if(buffReader !=null){
try{
buffReader.close();
return data;
}catch(Exception e){
e.printStackTrace();
}
}
}
}
}
编辑:刚刚添加了我的XML文件
这是XML文件
<RelativeLayout 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: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=".MainActivity" >
<TextView
android:id="@+id/tvHttpStuff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText1"
android:layout_marginTop="28dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:text="TextView" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvHttpStuff"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="http://website.com" >
<requestFocus />
</EditText>
<Button
android:id="@+id/getHtmlBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/editText1"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:text="Get HTML" />
我已经在一个网站上测试过了。它仍然不起作用。我希望有人能帮我解决这个问题。尝试更改httpresponse.settext网站;对于httpresponse.setTextSomething;并显示是否获得了在输出中写入的文本。如果没有,您应该发布布局xml文件 编辑: 尝试添加:
</RelativeLayout>
在xml文件的末尾。然后清理您的项目并重试,可能这个布局错误导致了错误的R.java构建。除非您更改了它,否则您无法在主线程上进行网络连接。您可能遇到NetworkOnMainThread异常,是否检查了日志 要获得更清晰的代码,请添加回调以处理按钮单击:
<Button
android:id="@+id/getHtmlBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/editText1"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:text="Get HTML"
android:onClick="onClickButtonGetHtml" />
编辑:确保您对清单具有网络权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
首先尝试在AsyncTask中从Web读取。您的文本视图可见吗?@TDMaster您能给我举个例子吗?我已经试过了,但没用。很有道理。有时候我不知道我的头在哪里哈哈我一定要试试这个。
<uses-permission android:name="android.permission.INTERNET"></uses-permission>