2017-11-25 5 views
-2

Ich bin irgendwie neu in Android-Entwicklung, und ich habe ein seltsames Problem.Android Httpurlconnection Problem - nichts passiert nach .connect

Der folgende Code soll arbeiten:

URL url = new URL("http://127.0.0.1/test.html"); 
HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 
connection.setRequestMethod("GET"); 
connection.connect(); 

int code = connection.getResponseCode(); 
System.out.println("code: "+code); 

Das Problem ist, nachdem ich die connection.connect tun(); nichts passiert, auch wenn ich nach der Verbindung eine textX.setText() hinzufügen, kann ich keine Aktion ausführen.

Jede Idee, was könnte das Problem sein?

Das ist meine ganze Methode, alles, was ich versuche, ist etwas Text von der API zu bekommen, der eigentlich "OK" sagt, aber ich kann es nicht funktionieren lassen.

public void conn (View view) 
    { 
     TextView text2 = (TextView)findViewById(R.id.text2); 
     text2.setText("connecting..."); 
     String output=""; 
     //All working until here 
     URL url; 
     HttpURLConnection urlConnection = null; 
     try { 
      output="about to connect"; 
      text2.setText(output); 
      url = new URL("http://127.0.0.1:4444/localweb/api/api.php"); 

      HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 

      connection.setRequestMethod("GET"); 
      connection.connect(); 
      InputStream in = urlConnection.getInputStream(); 
      text2.append("\nabout to get code"); 
      int code = connection.getResponseCode(); 
      text2.setText(Integer.toString(code)); 

      //urlConnection = (HttpURLConnection) url.openConnection(); 
      //urlConnection.connect(); 
      //output=urlConnection.getResponseMessage(); 
      //text2.setText(output); 


      InputStreamReader isw = new InputStreamReader(in); 

      int data = isw.read(); 
      while (data != -1) { 
       char current = (char) data; 
       data = isw.read(); 
       System.out.print(current); 
       output=output+current; 
      } 
      //text2.setText(output); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      if (urlConnection != null) { 
       urlConnection.disconnect(); 
      } 
     } 
    } 
+1

Bitte lernen Netzwerkgrundlagen - was localhost (127.0.0.1) bedeutet ... offensichtlich, wenn HTTP-Server nicht auf dem Gerät selbst ist, wird dies nicht funktionieren – Selvin

+0

@Selvin, 127.0.0.1 wurde gesetzt, es hier zu posten, es ist mein öffentliche IP, was rein geht – Marco

+0

Dann sollten Sie natürlich logcat Ausgabe lesen – Selvin

Antwort

0

Es war nicht etwas ‚falsch‘ mit dem HTTP-Client-Code, das Problem ist, dass Sie nicht die httpurlclient aus dem übergeordneten Thread starten können, wie ich zu tun versuchen, es muss im Hintergrund durch eine ausgeführt werden AsyncTask, nachdem ich alle httpurlconnection-Sachen in eine zusätzliche asynchrone Funktion verschoben habe, kann ich jetzt alle Web-Details bekommen, die ich brauchte.

Verwandte Themen