Issue
I try to connect https://stackoverflow.com/about. In command line connection is success . But when i connect with Android Gui+Genymotion , program was broken. Broken line is : urlConn.connect(); Error msg: Unfortunately,Merhaba has stopped. I added
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
but i still have a problem. Sorry my bad English.
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;
}
}
Solution
Try this:
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();
Answered By - Leon Parker
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.