2014-10-05 78 views
0

我尝试连接https://stackoverflow.com/about。在命令行连接是成功的。但是当我连接Android Gui + Genymotion时,程序被破坏了。断行是:urlConn.connect(); 错误消息:不幸的是,Merhaba已经停止。 我加了Eclipse中的Genymotion Http连接

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

但我仍然有问题。对不起我英语不好。

public class MainActivity extends Activity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
     String strUrl = "https://stackoverflow.com/about"; 
     try { 
      URL url = new URL(strUrl); 
      HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); 
      urlConn.connect(); 
      //System.out.println(HttpURLConnection.HTTP_OK); 
      //assertEquals(HttpURLConnection.HTTP_OK, urlConn.getResponseCode()); 
     } catch (IOException e) { 
      //System.err.println("Error creating HTTP connection"); 
      //System.out.println("Hata"); 
      //throw e; 
     }   
} 

回答

0

试试这个:

final String strUrl = "http://stackoverflow.com/about"; 

    Thread thread = new Thread(new Runnable(){ 
     @Override 
     public void run() { 
      try { 
       URL url = new URL(strUrl); 
        HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); 
        urlConn.connect(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 

    thread.start(); 
+1

非常感谢你 – 2014-10-05 18:42:09