2014-03-07 38 views
-1

我試圖從httpclient的幫助下從某個網站獲取數據,然後將該數據設置爲文本視圖,但不會發生。這裏就是我剛纔設置的TextView的文本將從其他類無法設置TextView的文本(使用httpclient獲取)

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> 
+0

它不會發生意義,它崩潰? – Raghunandan

+0

否否否。它運行完美。 Textview的文本並沒有改變 –

+0

,你包圍了用try-catch設置文本的代碼,它可能會拋出一個異常,並捕獲它:檢查logcat中是否打印了堆棧跟蹤 – donfuxx

回答

1

不要在Background網絡相關的工作使用AsyncTask像下面前:

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

並且不要忘記在manifest.xml

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