2016-03-21 21 views
0

Ich entwickle ein Programm, das HttpUrlConnection erfordert. Das Problem ist httpConn.connect() nie erfolgreich. Ich habe überprüft, ob die Netzwerkverbindung verfügbar ist und der Status zuvor verbunden ist.Android HttpUrlConnection Verbindung immer fehlgeschlagen

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

Das Hinzufügen dieses folgenden Codes in der onCreate-Methode verhindert nur das Schließen des Programms.

if (android.os.Build.VERSION.SDK_INT > 9) { 
     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
     StrictMode.setThreadPolicy(policy); 
    } 

Ich habe diese Berechtigungen in einem offenkundigen

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

hinzugefügt Warum es passieren kann und wie dieses Problem zu lösen?

+1

können Sie Fehlerprotokoll hinzufügen? –

+0

kann URL sein ist nicht korrekt! –

+2

Machst du es auf Ui-Thread? Wenn ja, dann tue es einen anderen Thread wie asynchrone Aufgabe –

Antwort

0

Es ist mein:

public class YourClass extends AsyncTask<String, String, String> { 

private AsyncResponse listener; //The listener interface 
HttpURLConnection conn = null; //New object HttpURLConnection 



public YourClass(AsyncResponse listener){ 
    this.listener=listener; 
    //Params you need in this class 
} 

protected void onPreExecute() { 
    //Job you need to do before execute 
} 


@Override 
protected String doInBackground(String... params) { 

    String response = ""; 
    String responseError = ""; 

    try{ 
     //Connect to URL 
     Log.d("JSON", "Start of connexion"); 
     URL url = new URL(params[0]); 
     conn = (HttpURLConnection) url.openConnection(); 
     conn.setReadTimeout(15000); 
     conn.setConnectTimeout(15000); 
     conn.connect(); 

     int responseCode=conn.getResponseCode(); 

     //HTTP_OK --> 200 
     //HTTP_CONFLICT --> 409 
     if (responseCode == HttpsURLConnection.HTTP_OK) { 
      String line; 
      BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream())); 
      while ((line=br.readLine()) != null) { 
       response+=line; 
      } 
      return response; 
     } 
     else if(responseCode == HttpURLConnection.HTTP_CONFLICT){ 
      String line; 
      BufferedReader br=new BufferedReader(new InputStreamReader(conn.getErrorStream())); 
      while ((line=br.readLine()) != null) { 
       responseError+=line; 
      } 
      return responseError; 
     } 
     else { 
      return null; 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     if (conn != null){ 
      conn.disconnect(); 
     } 

    } 

     return null; 
} 

    @Override 
    protected void onPostExecute(String result) { 
     try{ 
      super.onPostExecute(result); 
      listener.onTaskCompleted(result); 
     } 
     catch(NullPointerException npe){ 
      Log.d("NullPointerException", npe.getMessage()); 
     } 
    } 
} 

AsynResponse eine Schnittstelle ist, dass ich diese Schnittstelle implementiert, um die Klasse, die meine AsyncTask nennen, ich Methode OnTaskComplete außer Kraft setzen. Wenn die AsyncTask beendet ist, ist diese Methode Aufruf.

Weitere Informationen: How to get the result of OnPostExecute()


Wie ich diese Klasse in meiner Aktivität verwenden, die AsynResponse Schnittstelle implementiert:

new YourClass(this).execute("http://myUrl.com"); 

ich diese Hilfe hoffen.

Verwandte Themen