无法设置TextView的文本(使用httpclient获取)

时间:2014-03-07 08:02:05

标签: android

我试图在httpclient的帮助下从某个网站获取数据,然后将该数据设置为文本视图但不会发生。这是第一个我只是将textview的文本设置为将从其他类中获取的文本的类

public class HttpExample extends Activity {
    TextView httpStuff;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.httpex);

    httpStuff = (TextView) findViewById(R.id.tvHttp);
    GetMethodEx obj = new GetMethodEx();
    String result;
    try {
        result = obj.getInternetData();

        httpStuff.setText(result);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

这是我试图从中获取数据的类。来自httpclient的数据更具体

public class GetMethodEx {
    public String getInternetData() throws Exception {
        BufferedReader in = null;
        String data = null;
        try {
            HttpClient client = new DefaultHttpClient();
            URI website = new URI(
                    "http://www.mybringback.com/");
            HttpGet request = new HttpGet();
            request.setURI(website);
            HttpResponse response = client.execute(request);
            in = new BufferedReader(new InputStreamReader(response.getEntity()
                    .getContent()));
            StringBuffer sb = new StringBuffer("");
            String l = "";
            String nl = System.getProperty("line.separator");
            while ((l = in.readLine()) != null) {
                sb.append(l + nl);
            }
            in.close();
            data = sb.toString();
            return data;
        } finally {
            if (in != null) {
                try {
                    in.close();
                    return data;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

}

这是xml部分。我通过在某人要求我之前加入xml代码来更新问题

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layoutTwitter"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <TextView
            android:id="@+id/tvHttp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Loading Data" >
        </TextView>
    </ScrollView>

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

使用BackgroundAsyncTask中与您的网络相关的工作如下所示:

class AsyncTask2 extends AsyncTask<Void, Integer, String> {
BufferedReader in = null;
String data ="";
@Override
protected void onPreExecute() {
    super.onPreExecute();
}

@Override
protected String doInBackground(Void... aurl) {
        try{


    try {
        HttpClient client = new DefaultHttpClient();
        URI website = new URI(
                "http://www.mybringback.com/");
        HttpGet request = new HttpGet();
        request.setURI(website);
        HttpResponse response = client.execute(request);
        in = new BufferedReader(new InputStreamReader(response.getEntity()
                .getContent()));
        StringBuffer sb = new StringBuffer("");
        String l = "";
        String nl = System.getProperty("line.separator");
        while ((l = in.readLine()) != null) {
            sb.append(l + nl);
        }
        in.close();
        data = sb.toString();

    } finally {
        if (in != null) {
            try {
                in.close();
                return "Error";
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
return data;
}

    @Override
    protected void onPostExecute(String cnt) {
            httpStuff.setText(cnt);
    }
}

并称之为

new AsyncTask2().execute();

不要忘记在permission

中添加manifest.xml
<uses-permission android:name="android.permission.INTERNET" />
相关问题