2017-05-24 84 views
0

我嘗試連接我的電腦上的PHP,但Android代碼將停止在con.connect() 我不知道它會發生什麼。HttpURLConnection連接()將無法工作

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; 
} 

我發現其中一個問題是SSL認證?

的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 
+0

什麼是錯誤您收到? – Marc

+0

點擊按鈕後,logcat將只會在'connect()'之前和'finally {}'之前顯示'tag',換句話說,沒有錯誤響應 –

+0

您可以複製/粘貼相關的logcat嗎? – Marc

回答

1

您正在嘗試連接到localhost:8081這本身就是在Android設備。通過USB

使用adb到港口前進/後退到您的計算機,或者你可以簡單地與您的計算機的IP localhost替換本地網絡上。例如:192.168.0.2:8081

其他潛在的問題:

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

至少有a.printStackTrace()取代a.getMessage();打印錯誤。不過說真的,你應該使用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); 
    } 

還要確保您有以下添加到您的清單<uses-permission android:name="android.permission.INTERNET"/>

+0

謝謝,我已添加'<使用權限android:name =「android.permission.INTERNET」/>'到清單,並通過我的主機名使用URL(從no-ip) 。我會改變'catch exception'語句。 –

+0

謝謝,我收到錯誤消息:IO錯誤java.net.ConnectException:10000ms後無法連接到本地/ 127.0.0.1(端口8081):isConnected失敗:ECONNREFUSED(連接被拒絕)'我會盡力解決它,謝謝很多 –

+0

您無法連接到'localhost',因爲您沒有在Android設備上運行網絡服務器。 – ejohansson