2011-11-02 110 views
0

我想用httpclient從網上讀取一個文件,並得到這個錯誤。我究竟做錯了什麼?代碼示例將不勝感激。用HttpClient從網上讀取文件

11-02 17:21:09.845: WARN/ActivityManager(67): Launch timeout has expired, giving up wake lock! 
11-02 17:21:10.464: WARN/ActivityManager(67): Activity idle timeout for HistoryRecord{405591c0 com.jeffbreunig/.RetrievingAmazonXMLDataActivity} 
11-02 17:21:15.534: DEBUG/dalvikvm(310): GC_EXPLICIT freed 7K, 54% free 2537K/5511K, external 1625K/2137K, paused 64ms 
11-02 17:21:18.244: DEBUG/SntpClient(67): request time failed: java.net.SocketException: Address family not supported by protocol 
11-02 17:21:20.998: DEBUG/(1879): DID NOT WORK 
11-02 17:21:21.344: INFO/ActivityManager(67): Displayed com.jeffbreunig/.RetrievingAmazonXMLDataActivity: +21s38ms 

代碼:

public class RetrievingAmazonXMLDataActivity extends Activity { 

    private static final String TAG = null; 
    TextView txt; 


    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


     txt = (TextView) findViewById(R.id.txt); 

     String urlToRead = "http://webdev4.matcmadison.edu/mbtest/mab/AmazonXML/FileList.txt"; 
     try { 
     String fileList = getContent(urlToRead); 

     txt.setText(fileList); 

     } catch (Exception e) { 
      Log.println(Log.DEBUG, TAG, "DUMP FILES DID NOT WORK"); 
     } 



    } 



    public String getContent(String url) { 

     String responseBody; 

     try { 
      HttpClient client = new DefaultHttpClient(); 
      ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
      HttpGet getMethod = new HttpGet(url); 
      responseBody = client.execute(getMethod, responseHandler); 
     } 
     catch (Exception ex) { 
     Log.println(Log.DEBUG, TAG,"DID NOT WORK"); 
      Toast.makeText(this, "Request failed: " + ex.getMessage(), 6000).show(); 
      return "Failure to read from server!"; 
     } 

     return responseBody; 

    } 

    public void onItemSelected(AdapterView<?> parent, View v, int position, long id) 
    { 
     String s = String.format("You chose [%s]", items[position]); 
    } 

    public void onNothingSelected(AdapterView<?>parent) { 

    } 


} 

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?> 
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
      package="com.jeffbreunig" 
      android:versionCode="1" 
      android:versionName="1.0"> 
     <uses-sdk android:minSdkVersion="9" /> 
     <uses-permission android:name="android.permission.INTERNET"/> 

     <application android:icon="@drawable/icon" android:label="@string/app_name"> 
      <activity android:name=".RetrievingAmazonXMLDataActivity" 
         android:label="@string/app_name" > 
       <intent-filter> 
        <action android:name="android.intent.action.MAIN" /> 
        <category android:name="android.intent.category.LAUNCHER" /> 
       </intent-filter> 
      </activity> 

     </application> 
    </manifest> 

回答

0

好propably問題是您在Android 3.0及以上這樣做,對不對? 在那裏,你不得不使用AsyncClass來完成web服務或從互聯網上下載一些東西,因爲你的主UI線程不能沒有反應(UI長度可以被長時間運行的代碼「鎖定」多長時間); - )

+0

是的,是Android 3.0,也嘗試過與Android 2.1相同的結果 – Tom

1

你必須使用某種線程機制來通過http獲取數據。我建議一個AsyncTask