2016-05-19 15 views
0

Ich bin nicht vertraut mit der HttpUrlConnection auf Android Studio. Ich habe versucht, mit HttpClient in Eclipse zu codieren und es funktioniert. HttpClient ist jetzt in Android Studio veraltet, also habe ich versucht, es mit der HttpUrlConnection zu ändern. Mein Code in Android Studio ist wie folgt:HttpUrlConnection in Android kein Fehler, aber nicht funktioniert

public String LedOff(View view) { 
    try { 
     URL url = new URL("http://192.168.4.1/LEDOff"); 
     HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
     urlConnection.connect(); 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

}

in Eclipse es so ist:

public void LedOff (View view){ 

     String url = "http://192.168.4.1/LEDOff"; 

     HttpClient httpClient = new DefaultHttpClient(); 

     try { 
      httpClient.execute(new HttpGet(url)); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
} 

Ich weiß nicht, was los ist. Bitte hilf mir, Jungs. Vielen Dank.

+0

bitte Ihre logcat einfügen .. – prat

+0

Sind Sie hinter einem Proxy? –

+0

05-19 15: 27: 12,727 21143-21150/com.ganda.alex.http D/jdwp: handlePacket: cmd = 0x1, cmdSet = 0xC7, len = 0x14, id = 0x400067C7, flags = 0x0, dataLen = 0x9 05-19 15: 27: 12,727 21143-21150/com.ganda.alex.http D/jdwp: sendBufferedRequest: len = 0x34 05-19 15: 27: 13,228 21143-21150/com.ganda.alex.http D/jdwp: processIncoming mein Logcat ist einfach so über alle – Alexxx

Antwort

1

Sie müssen die Anforderung bestätigen, indem Sie getInputStream(), getResponseCode() oder getResponseMessage() aufrufen, damit die Antwort zurückgegeben und verarbeitet werden kann.

Connect() Methode erstellt nur die Verbindung.

prüfen mit:

Log.i("Response Code" , urlConnection .getResponseCode()); 

sehen, welche Antwort bekommen Sie.

0

versuchen, wie diese

URL url = new URL("http://192.168.4.1/LEDOff"); 
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
    urlConnection.setDoInput(true); 
    urlConnection.setDoOutput(true) 
    urlConnection.connect(); 
    if(urlConnection.getResponseCode()==200) 
    { 
    ... 
    } 
1

Stellen Sie sicher, dass diese beiden Berechtigungen in Ihrem Manifest haben:

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

bearbeiten
Added Code LedOff() Methode eine erfolgreiche Antwort als String zu behandeln .

Und das versuchen, per Android Docs:

public String LedOff(View view) { 
    try { 
     URL url = new URL("http://192.168.4.1/LEDOff"); 
     HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
     conn.setReadTimeout(10000 /* milliseconds */); 
     conn.setConnectTimeout(15000 /* milliseconds */); 
     conn.setRequestMethod("GET"); // Or any method you need 
     conn.setDoInput(true); 

     // Starts the query 
     conn.connect(); 
     int response = conn.getResponseCode(); 
     Log.d(DEBUG_TAG, "The response code is: " + response); 

     if ((response >= 200) && (response < 300)) { 
      // We are assuming here that whatever the response is, it can be parsed as a String 
      Log.d(DEBUG_TAG, "The response is: " + conn.getResponseMessage()); 
     } 
    } catch(Exception ex) { 
     // Handle any exceptions 
    } 
} 
+0

kann mir bitte ein Beispiel für die Antwort geben. Es tut mir so leid, weil ich hier nur ein Neuling bin. Das ist das erste Mal, dass ich HTTP-Verbindungen einstelle. – Alexxx

+0

Was erwarten Sie genau, wenn Sie eine Verbindung zu "** http: //192.168.4.1/LEDOff**" herstellen? –

+0

Ich habe meine Antwort bearbeitet und Code eingeschlossen, um eine erfolgreiche Antwort als 'String' zu behandeln. –

Verwandte Themen