I'm trying to make a Connection to a server. But it didn't connect or make a connection.
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends ActionBarActivity {
TextView text;
String http_url = "http://sampleprogramz.com";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.textView2);
try {
URL url = new URL(http_url);
executeReq(url);
text.setText("HttpURLConnection Available");
}
catch(Exception e) {
text.setText("Connection Failed");
}
}
private void executeReq(URL url) throws IOException {
// TODO Auto-generated method stub
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setReadTimeout(3000);
con.setConnectTimeout(3500);
con.setRequestMethod("GET");
con.setDoInput(true);
// Connect
con.connect();
}
}
Here I'm trying to ask the program if there is a connection. I also have the Internet Permission in my AndroidManifest.xml file. Do you have any Idea why this wont work?
Here i have the files of my LogCat, but i'm not sure if this are the right files, becaue i'm new:
08-06 16:12:15.931: I/art(29675): Background sticky concurrent mark sweep GC freed 4988(324KB) AllocSpace objects, 0(0B) LOS objects, 28% free, 798KB/1117KB, paused 6.482ms total 239.323ms
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): I got an error
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): android.os.NetworkOnMainThreadException
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at java.net.InetAddress.getAllByName(InetAddress.java:215)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:272)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at com.example.david.simpleserverrequest.MainActivity.executeReq(MainActivity.java:54)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at com.example.david.simpleserverrequest.MainActivity.onCreate(MainActivity.java:31)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at android.app.Activity.performCreate(Activity.java:5990)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at android.app.ActivityThread.access$800(ActivityThread.java:151)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at android.os.Handler.dispatchMessage(Handler.java:102)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at android.os.Looper.loop(Looper.java:135)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at android.app.ActivityThread.main(ActivityThread.java:5257)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at java.lang.reflect.Method.invoke(Native Method)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at java.lang.reflect.Method.invoke(Method.java:372)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
08-06 16:12:16.068: E/YOUR_APP_LOG_TAG(29675): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
08-06 16:12:17.427: D/gralloc_goldfish(29675): Emulator without GPU emulation detected.
08-06 16:12:17.451: I/art(29675): Background partial concurrent mark sweep GC freed 882(120KB) AllocSpace objects, 0(0B) LOS objects, 52% free, 910KB/1934KB, paused 1.354ms total 284.542ms