2016-06-02 17 views
0

Ich entwickle eine Anwendung mit Android Studio, ich bin völlig neu in Android und das ist meine zweite Anwendung, ich habe überall gesucht, um einen Weg zu finden, eine URL zu öffnen und zu lese den JSON-Inhalt daraus, das sind die Codes, die ich gefunden habe, aber das Problem ist, wo es zu "con.connect()" kommt; es sagt Leider hat Ihre Anwendung funktioniert nicht ich kann nicht sogar die Schnur von dieser URL lesen, ich weiß wirklich nicht, was das Problem ist, also hoffe ich, dass Sie mir helfen können, dieses Problem zu lösen.HttpsURLConnection funktioniert nicht in android Studio

public String webrequest(String url1){ 
    try { 

     URL url = new URL(url1); 
     HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); 
     con.connect(); 
     if (con != null) { 
      try { 
       BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(),"UTF-8")); 
       String input; 
       while ((input = br.readLine()) != null) { 
       System.out.println(input); 
       } 
       br.close(); 
       return input.toString(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    //return input; 
    return ""; 
} 

Und übrigens, ich habe die Internet-Erlaubnis in android Manifest. Vielen Dank für Ihre Hilfe.

+1

Haben Sie diesen Code außerhalb der Hauptlauf (UI) Thread? Haben Sie AsyncTask oder einen ähnlichen Thread-orientierten Ansatz verwendet? – devnull69

Antwort

-3

Fügen Sie useLibrary 'org.apache.http.legacy' in App build.gradle in Android-Abschnitt hinzu.

Wie unten gezeigt:

android { 
     compileSdkVersion 23 
     buildToolsVersion "23.0.2" 

     defaultConfig { 
      applicationId "com.test.smapp" 
      minSdkVersion 14 
      targetSdkVersion 23 
     } 
     packagingOptions { 
      exclude 'META-INF/NOTICE.txt' 
     } 

     buildTypes { 
      release { 
       minifyEnabled false 
       proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt' 
      } 
     } 
     useLibrary 'org.apache.http.legacy' 
    } 
+0

Das OP möchte HttpsURLConnection verwenden, bitte respektiere diese Absichten. –

1

dieses Formiat Versuchen -

private InputStream openHttpConnection(String urlStr) { 
    InputStream in = null; 
    int resCode = -1; 

    try { 
    URL url = new URL(urlStr); 
    URLConnection urlConn = url.openConnection(); 

    if (!(urlConn instanceof HttpURLConnection)) { 
     throw new IOException("URL is not an Http URL"); 
    } 
    HttpURLConnection httpConn = (HttpURLConnection) urlConn; 
    httpConn.setAllowUserInteraction(false); 
    httpConn.setInstanceFollowRedirects(true); 
    httpConn.setRequestMethod("GET"); 
    httpConn.connect(); 
    resCode = httpConn.getResponseCode(); 

    if (resCode == HttpURLConnection.HTTP_OK) { 
     in = httpConn.getInputStream(); 
    } 
    } 

    catch (MalformedURLException e) { 
    e.printStackTrace(); 
    } 

    catch (IOException e) { 
    e.printStackTrace(); 
    } 
    return in; 
} 
+0

Was ist mit 'HttpsURLConnection'? –

Verwandte Themen