0

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
  • How do you know that you are not connecting? – Arlind Hajredinaj Aug 06 '15 at 15:13
  • 2
    What is the error message? However, you should put your connection code into asynctask – BNK Aug 06 '15 at 15:14
  • Please add `e.printStackTrace()` in your catch clause and then edit the output into your question so we can actually help you instead of playing 20 questions. – d0nut Aug 06 '15 at 15:18
  • 1
    This is almost certainly http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception – adelphus Aug 06 '15 at 15:20
  • @Arlind it says Connection Failure with the settext Methode – kaeptenlook Aug 06 '15 at 15:30
  • @kaeptenlook post the Exceptions message – Arlind Hajredinaj Aug 06 '15 at 15:36
  • @Arlind how can i see the e.printStackTrace with Android ? normally i do it with e.getMessage and in this case it says : null – kaeptenlook Aug 06 '15 at 15:44
  • @kaeptenlook You can use debugging or log the exception on LogCat here is a link http://stackoverflow.com/questions/4341363/android-print-full-exception-backtrace-to-log – Arlind Hajredinaj Aug 06 '15 at 15:46
  • @Arlind i'm not sure if you mean this, but i tried a bit with the logCat but the message is to long – kaeptenlook Aug 06 '15 at 16:13
  • @Arlind i have put the Messages of the LogCat in my post – kaeptenlook Aug 06 '15 at 16:18
  • @kaeptenlook The exception being thrown is NetworkOnMainThreadException you should never do long running operations on the UIThread, This is my answer on connecting to the internet to fetch some data look at the AsyncTask part http://stackoverflow.com/questions/29571680/putting-json-data-into-a-list/29572036#29572036 – Arlind Hajredinaj Aug 06 '15 at 16:33

2 Answers2

0

You are trying to call a method on the main thread. You will need to either execute this in a background handler or on a new thread.

dave
  • 1,410
  • 1
  • 16
  • 42
-1

Instead of doing it manually there is a library available which will handle connection and also handle rest of all connection related issues.Try using http://loopj.com/android-async-http/

Adarsh Yadav
  • 3,752
  • 3
  • 24
  • 46