2010-07-28 4 views
1

Je suis un code pour connecter la page Web, mais il montre toujoursNon Se connecter à la page Web sur Android

"Échec de la connexion, l'hôte est non résolue: www.streetcar.org:80"

Le code est le suivant.

package myapp.httpdwnd; 

import android.app.Activity; 
import java.io.InputStream; 
import android.os.Bundle; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
import java.io.IOException; 
import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 


public class httpdwndActivity extends Activity 
{ 
     TextView txt; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
    super.onCreate(savedInstanceState); 
    LinearLayout rootLayout = new LinearLayout(getApplicationContext()); 
      txt = new TextView(getApplicationContext()); 
      rootLayout.addView(txt); 
      setContentView(rootLayout); 
       txt.setText("Connecting..."); 
         txt.setText(connect("http://www.streetcar.org/mim/cable/images/cable-01.jpg")); 
    } 

    private String connect(String url){ 

       // Create the httpclient 
       HttpClient httpclient = new DefaultHttpClient(); 

       // Prepare a request object 
       HttpGet httpget = new HttpGet(url); 

       // Execute the request 
       HttpResponse response; 

       // return string 
       String returnString = null; 

       try { 

        // Open the webpage. 
        response = httpclient.execute(httpget); 

        if(response.getStatusLine().getStatusCode() == 200){ 
         // Connection was established. Get the content. 

         HttpEntity entity = response.getEntity(); 
         // If the response does not enclose an entity, there is no need 
         // to worry about connection release 

         if (entity != null) { 
          // A Simple JSON Response Read 
          InputStream instream = entity.getContent(); 

          // Load the requested page converted to a string into a JSONObject. 
          JSONObject myAwway = new JSONObject(convertStreamToString(instream)); 

          // Get the query value' 
          String query = myAwway.getString("query"); 

          // Make array of the suggestions 
          JSONArray suggestions = myAwway.getJSONArray("suggestions"); 

          // Build the return string. 
          returnString = "Found: " + suggestions.length() + " locations for " + query; 
          for (int i = 0; i < suggestions.length(); i++) { 
           returnString += "\n\t" + suggestions.getString(i); 
          } 

          // Close the stream. 
          instream.close(); 
         } 
        } 
        else { 
         // code here for a response other than 200. A response 200 means the webpage was ok 
         // Other codes include 404 - not found, 301 - redirect etc... 
         // Display the response line. 
         returnString = "Unable to load page - " + response.getStatusLine(); 
        } 
       } 
       catch (IOException ex) { 
        // thrown by line 80 - getContent(); 
        // Connection was not established 
        returnString = "Connection failed; " + ex.getMessage(); 
       } 
       catch (JSONException ex){ 
        // JSON errors 
        returnString = "JSON failed; " + ex.getMessage(); 
       } 
       return returnString; 
      } 

      private static String convertStreamToString(InputStream is) { 
       /* 
       * To convert the InputStream to String we use the BufferedReader.readLine() 
       * method. We iterate until the BufferedReader return null which means 
       * there's no more data to read. Each line will appended to a StringBuilder 
       * and returned as String. 
       */ 
       BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
       StringBuilder sb = new StringBuilder(); 

       String line = null; 
       try { 
        while ((line = reader.readLine()) != null) { 
         sb.append(line + "\n"); 
        } 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } finally { 
        try { 
         is.close(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
       } 
       return sb.toString(); 
      } } 

Quelle peut-être l'erreur dans ce?

Aidez-nous s'il vous plaît.

Merci.

Répondre

1

Il peut être becoz de "hôte ne reconnaît pas" et, par conséquent UnknownHostException est élevé ..

Traverser cette page: http://developer.android.com/reference/java/net/UnknownHostException.html#UnknownHostException%28%29

OU

contrôle sur l'autorisation "Internet" dans votre fichier AndroidMenifest.xml, si vous n'avez pas ajouté, ajoutez ce qui suit:

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

merci mayani, je l'ai utilisé comme vous l'avez dit, mais il n'y avait encore aucune amélioration – adithi

0

"L'hôte n'est pas résolu" signifie que la recherche DNS pour votre serveur a échoué. Dans l'émulateur, cela peut signifier que vous n'avez pas de connexion Internet - vérifiez si d'autres applications (par exemple, le navigateur) fonctionnent. Vérifiez également si l'application Navigateur peut accéder à www.streetcar.org.

+0

merci je l'ai vérifié out.i avoir une connexion et essayé dans d'autres navigateurs pour ouvrir ce site. – adithi

Questions connexes