0

I try to connect the php on my PC , but the Android code will stop at con.connect() I have no idea what happen it is.

 public String doInBackground(Void... arg0){
    HttpURLConnection con = null;
    String urlString = "http://localhost:8081/PHP/Android_SQL.php";
    System.out.println("before try");

    String res = "";
    try {
        URL url = new URL(urlString);
        con = (HttpURLConnection) url.openConnection();
        con.setReadTimeout(15000);
        con.setConnectTimeout(10000);
        con.setRequestMethod("GET");

        //set input and output descript
        con.setDoInput(true);

        con.setDoOutput(false); // in needed?
        System.out.println("connecting");
        con.connect(); // won't work
        System.out.println("connect ");
        System.out.println(con.getURL());

        BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
            sb.append(line + "\n");
            System.out.println(line);
        }


        json = sb.toString();
        br.close();
        return json;

    }catch (SocketTimeoutException a){
        a.getMessage();
    }catch (IOException b){
        b.getMessage();
    }catch (Exception e) {
        e.getMessage();
    }
    finally {
        System.out.println("sueecssful");
    }
    return null;
}

I find one of the problem is SSL certification?

The logcat:

05-24 20:27:44.032 3089-3089/com.example.user.tophp I/System.out: click the button
05-24 20:27:44.033 3089-3237/com.example.user.tophp I/System.out: before try
05-24 20:27:44.033 1524-1595/system_process W/AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client
05-24 20:27:44.033 3089-3237/com.example.user.tophp I/System.out: connecting
05-24 20:27:44.036 3089-3237/com.example.user.tophp I/System.out: sueecssful
Zi-yan Tseng
  • 184
  • 1
  • 1
  • 14
  • What is the error you are getting? – Marc May 24 '17 at 20:16
  • after click the button , the logcat will only show the `tag` before `connect()` and after `finally{ }` , in other words , no error response – Zi-yan Tseng May 24 '17 at 20:23
  • Can you copy/paste the relevant logcat, please? – Marc May 24 '17 at 20:24
  • @Marc i edit the answer to show the logcat – Zi-yan Tseng May 24 '17 at 20:31
  • Check the following URL. That might help you: https://stackoverflow.com/questions/2151359/java-httpurlconnection-doesnt-connect-when-i-call-connect – Marc May 24 '17 at 20:35
  • @Marc well, i got the article after i ask . However , I just follow the step by google "httpurlconnection to php" , and get no working as you look – Zi-yan Tseng May 24 '17 at 20:38
  • Oh, read the answer below by ejohansson. I must be tired. – Marc May 24 '17 at 20:40
  • @Marc `IO Error java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 8081) after 10000ms: isConnected failed: ECONNREFUSED (Connection refused)` thanks, after change the `catch exception` i get these message , i will try to solve it , thanks – Zi-yan Tseng May 24 '17 at 20:49

1 Answers1

2

You are trying to connect to localhost:8081 which itself is the Android device.

Use adb to to port forward/reverse to your computer via the USB or you can simply replace localhost with the IP of your computer on your local network. Example: 192.168.0.2:8081

Other potential issues:

catch (SocketTimeoutException a){
        a.getMessage();
    }catch (IOException b){
        b.getMessage();
    }catch (Exception e) {
        e.getMessage();
    }

At least replace a.getMessage(); with a.printStackTrace() to print the errors. But really you should use Log.e("MyTag", "Error", a); * https://developer.android.com/reference/android/util/Log.html

catch (SocketTimeoutException a){
        Log.e("MyTag", "Timout Error", a);
    }catch (IOException b){
        Log.e("MyTag", "IO Error", b);
    }catch (Exception e) {
        Log.e("MyTag", "Error", e);
    }

Also make sure you have the following added to your manifest <uses-permission android:name="android.permission.INTERNET"/>

ejohansson
  • 2,832
  • 1
  • 23
  • 30
  • thanks , i had add `` into manifest , and use url by my host name ( from no-ip). I'll change the `catch exception`statement. – Zi-yan Tseng May 24 '17 at 20:42
  • thanks , i get the error message `IO Error java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 8081) after 10000ms: isConnected failed: ECONNREFUSED (Connection refused)` I will try to solve it , thanks a lot – Zi-yan Tseng May 24 '17 at 20:49
  • You cannot connect to `localhost`, because you are not running the webserver on your android device. – ejohansson May 24 '17 at 20:56