不要在TextView上获得任何输出

时间:2014-05-06 20:03:53

标签: java android android-edittext textview

我制作了一个小应用程序,它返回一个网站的html进行练习。但是我没有在textview上获得输出。这是代码:

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" />

我已经用网站测试了它。它仍然没有奏效。我希望有人可以帮助我。

2 个答案:

答案 0 :(得分:0)

尝试更改httpresponse.setText(网站);对于httpresponse.setText(&#34; Something&#34;);并显示您是否获得了您在输出中写入的文本。如果不是,您应该发布您的布局xml文件。

编辑: 尝试添加:

</RelativeLayout>

在xml文件的末尾。然后清理你的项目并再试一次,也许这个布局错误导致一个坏的R.java构建。

答案 1 :(得分:0)

除非你改变它(你没有改变),否则你不能在主线程上进行网络连接。您可能遇到NetworkOnMainThreadException,是否检查了日志?

要获得更清晰的代码,请添加回调以处理按钮单击:

<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" />

然后你需要一个新线程来做网络工作。如果没有问题(调用getInternetData()的任何异常),您应该使用UI线程来更新TextView。我没有碰到你的getInternetData()。

public class MainActivity extends Activity {

    TextView httpresponse;
    EditText webAddress;
    String website;
    HttpClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webAddress = (EditText) findViewById(R.id.editText1);
        httpresponse = (TextView) findViewById(R.id.tvHttpStuff);   
    }

    public void onClickButtonGetHtml(View view) {

        // get the website URL from the EditText
        website = webAddress.getText().toString();

        // use a new thread to do the network stuff 
        new Thread(new Runnable() {

            @Override
            public void run() {

                try {
                    // get the HTML
                    final String returned = getInternetData();
                    // update your TextView using the UI Thread
                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            httpresponse.setText(returned);
                        }
                    });
                }
                catch(Exception ex) { ex.printStackTrace();}
            }
        }).start();
    }

    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();

                }
            }
        }
    }
}

编辑:确保您拥有清单上的网络权限。

<uses-permission android:name="android.permission.INTERNET"></uses-permission>