2011-04-20 47 views
1

我有一段代码,在我的活动(我开始这个来自模拟器)加载文件 - 拒绝连接

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    TextView txt=(TextView)this.findViewById(R.id.lbl); 
    try { 

      URL url = new URL("http://localhost/test.json"); 
      URLConnection urlconnection = url.openConnection(); 
      long l = urlconnection.getContentLength(); 

      BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); 
      String line; 
      StringBuffer buffer=new StringBuffer(); 
      while ((line = in.readLine()) != null) 
      { 
       buffer.append(line); 
      } 
      in.close(); 
      System.out.println("bufer="+buffer.toString()); 
      txt.setText(buffer.toString()); 


     } catch (Exception exc) { 

      exc.printStackTrace(); 

     } 
} 

和我的清单看起来像

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="test_android.test" 
     android:versionCode="1" 
     android:versionName="1.0"> 


    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity android:name=".test_android" 
        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> 
    <uses-permission android:name="android.permission.INTERNET"></uses-permission> 
</manifest> 

我有WAMP服务器我的万维网目录test.json,但我总是得到错误CONNECTION REFUSED。哪里不对 ?

+0

本地主机上你计算机不是Android设备/模拟器上的本地主机! – 2011-04-20 22:19:18

回答