HttpHostConnectException: Connexion refusée Android

Je suis en train d'essayer de se connecter via HttpPost et envoyer un nom d'utilisateur et mot de passe pour un site web et de recevoir une chaîne de caractères à partir de ce site web. J'ai essayé diverses méthodes qui ont fonctionné pour moi dans le passé, mais maintenant, quand j'ai envoyer le nom d'utilisateur et le mot de passe identifiants l'application les temps, tant que les 4 minutes et puis crache l'exception suivante:

 07-16 16:32:32.897: W/System.err(632): Unable to connect to the server
07-16 16:32:32.907: W/System.err(632): org.apache.http.conn.HttpHostConnectException: Connection to http://devdashboard.company refused
07-16 16:32:32.917: W/System.err(632):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
07-16 16:32:32.917: W/System.err(632):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-16 16:32:32.917: W/System.err(632):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-16 16:32:32.917: W/System.err(632):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
07-16 16:32:32.917: W/System.err(632):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-16 16:32:32.917: W/System.err(632):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-16 16:32:32.927: W/System.err(632):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-16 16:32:32.927: W/System.err(632):  at company.android.dashboard.app.HttpHelperAndroid.sendToHttp(HttpHelperAndroid.java:66)
07-16 16:32:32.927: W/System.err(632):  at company.android.dashboard.app.DashboardAppActivity.goToDashboard(DashboardAppActivity.java:62)
07-16 16:32:32.927: W/System.err(632):  at java.lang.reflect.Method.invokeNative(Native Method)
07-16 16:32:32.937: W/System.err(632):  at java.lang.reflect.Method.invoke(Method.java:511)
07-16 16:32:32.937: W/System.err(632):  at android.view.View$1.onClick(View.java:3039)
07-16 16:32:32.947: W/System.err(632):  at android.view.View.performClick(View.java:3511)
07-16 16:32:32.947: W/System.err(632):  at android.view.View$PerformClick.run(View.java:14105)
07-16 16:32:32.947: W/System.err(632):  at android.os.Handler.handleCallback(Handler.java:605)
07-16 16:32:32.957: W/System.err(632):  at android.os.Handler.dispatchMessage(Handler.java:92)
07-16 16:32:32.957: W/System.err(632):  at android.os.Looper.loop(Looper.java:137)
07-16 16:32:32.967: W/System.err(632):  at android.app.ActivityThread.main(ActivityThread.java:4424)
07-16 16:32:32.977: W/System.err(632):  at java.lang.reflect.Method.invokeNative(Native Method)
07-16 16:32:32.977: W/System.err(632):  at java.lang.reflect.Method.invoke(Method.java:511)
07-16 16:32:32.977: W/System.err(632):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-16 16:32:32.987: W/System.err(632):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-16 16:32:32.987: W/System.err(632):  at dalvik.system.NativeStart.main(Native Method)
07-16 16:32:32.987: W/System.err(632): Caused by: java.net.ConnectException: failed to connect to /50.19.240.232 (port 80): connect failed: ETIMEDOUT (Connection timed out)
07-16 16:32:32.997: W/System.err(632):  at libcore.io.IoBridge.connect(IoBridge.java:114)
07-16 16:32:32.997: W/System.err(632):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
07-16 16:32:32.997: W/System.err(632):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
07-16 16:32:33.007: W/System.err(632):  at java.net.Socket.connect(Socket.java:842)
07-16 16:32:33.007: W/System.err(632):  at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
07-16 16:32:33.017: W/System.err(632):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
07-16 16:32:33.017: W/System.err(632):  ... 22 more
07-16 16:32:33.027: W/System.err(632): Caused by: libcore.io.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
07-16 16:32:33.047: W/System.err(632):  at libcore.io.Posix.connect(Native Method)
07-16 16:32:33.047: W/System.err(632):  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
07-16 16:32:33.047: W/System.err(632):  at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
07-16 16:32:33.057: W/System.err(632):  at libcore.io.IoBridge.connect(IoBridge.java:112)
07-1
6 16:32:33.057: W/System.err(632):  ... 27 more

Internet autorisation EST activée dans mon fichier manifeste XML

Mon actuel de la mise en œuvre va comme ceci:

  String LOGIN = "[email protected]";
String PASSWORD ="password1";
//JSONObject to send the username and pw
JSONObject json = new JSONObject();
//put the path in the JSONArray object
JSONArray vect = new JSONArray();
vect.put("company Android Library");
vect.put("Rocket Ship");
int duration = 50;
try {
json.put("email", LOGIN);
json.put("password", PASSWORD);
json.put("json", "true");
} catch (JSONException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
Log.d(TAG, "ABOUT TO SEND:" + json.toString());
JSONObject inJson = HttpHelperAndroid.sendToHttp(json, "http://devdashboard.company/login");
if(inJson != null)
{
Log.d(TAG, "RECIEVED the JSON:" + inJson.toString());
}
else
Log.d(TAG, "THE RESPONSE WAS NULL");
}

Et la HttpHelperAndroid classe ressemble à ceci:

     public class HttpHelperAndroid
{
private static final String TAG = "HttpHelperAndroid";//TAG for the LogCat(debugging)
private static boolean responseSuccessful = true;
/**
* sends the JSONObject parameter to the desired URL parameter and gets the response
* 
* @param url the URL to which the JSONObject should be sent
* @param jsonObjOut the JSONObject that is to be sent
* @return the response from the server as a JSONObject
*/
public static JSONObject sendToHttp(JSONObject jsonObjOut, String url) {
responseSuccessful = true;
try
{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpRequest = new HttpPost(url);
//convert the JSONObject to a string
StringEntity se;
//set our StringEntity to the JSONObject as a string
se = new StringEntity(jsonObjOut.toString());
//Set HTTP params
httpRequest.setEntity(se);
httpRequest.setHeader("Accept", "application/json");
httpRequest.setHeader("Content-type", "application/json");
httpRequest.setHeader("Accept-Encoding", "gzip"); //for gzip compression
//get the current time
long oldTime = System.currentTimeMillis();
HttpResponse response = null;
try
{
//execute the http request and get the response
response = (HttpResponse) httpClient.execute(httpRequest);
}
catch(HttpHostConnectException e)
{
System.err.println("Unable to connect to the server");
e.printStackTrace();
responseSuccessful = false;
}
//only continue executing if we got a response from the server
if(responseSuccessful)
{
//print how long the response took to the LogCat if it's on
Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis()-oldTime) + "ms]");
//Get hold of the response entity (-> the data):
HttpEntity entity = response.getEntity();
if (entity != null) {
//Read the content stream
InputStream in = entity.getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
in = new GZIPInputStream(in);
}
//convert content stream to a String
String resultString= streamToString(in);
//close the stream
in.close();
//convert the String into a JSONObject
JSONObject jsonObjRecv = new JSONObject(resultString);
//take a peak at the JSONObject we got back if the LogCat is on
Log.i(TAG,"<JSONObject>\n"+jsonObjRecv.toString()+"\n</JSONObject>");
//return the JSONObject we got back from the server
return jsonObjRecv;
} 
}
}
//catch any exception that was thrown
catch (Exception e)
{
//Print the exception
e.printStackTrace();
}
return null;
}
private static String streamToString(InputStream is)
{
//create a new BufferedReader for the input stream
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
//create a new StringBuilder to append the lines
StringBuilder sb = new StringBuilder();
//initialize an empty string
String line = null;
try
{
//iterate as long as there is still lines to be read
while ((line = reader.readLine()) != null) 
{
//append the line and a newline character to our StringBuilder
sb.append(line + "\n");
}
}
//catch an IOException and print it
catch (IOException e) {
e.printStackTrace();
} 
//close the stream when we're done
finally 
{
try 
{
is.close();
} 
//catch and print an exception if it's thrown
catch (IOException e) 
{
e.printStackTrace();
}
}
//return the stream converted to a string
return sb.toString();
}
}

Et voici mon XML, juste pour le plaisir:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="company.android.dashboard.app"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="@drawable/company_android_ico"
android:label="@string/app_name" >
<activity
android:name=".DashboardAppActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

J'ai utilisé le HttpHelper classe dans les projets passés et il a travaillé pour moi, en plus j'ai essayé de mettre en œuvre cette aide nameValuePairs:

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);  
nameValuePairs.add(new BasicNameValuePair("email", "[email protected]"));  
nameValuePairs.add(new BasicNameValuePair("password", "password1"));
nameValuePairs.add(new BasicNameValuePair("json", "true"));  
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

Et cela a donné le même résultat.

- Ce que cela pourrait être en quelque sorte un certificat chose? ou peut-être quelque chose à faire avec une corruption du fichier XML( j'ai essayé de refaire le projet et le fichier xml) Android HTTP Connexion refusée
Ou peut-être une sorte de Android fichier hosts problème?

Je suis ouvert à toutes suggestions!

J'ai examiné cet de beaucoup d'angles et je suis heureux de fournir toute autre information qui pourrait être utile! J'apprécie vraiment votre temps!

REMARQUE: L'url est un mannequin url, et non pas le réel, je me connecte, pour des raisons de sécurité. Je suis en mesure de boucler le site web réel à partir de la ligne de commande avec les paramètres et ça fonctionne et je suis également capable de se connecter normalement à partir du navigateur web.

MODIFIER j'ai identifié le problème! Mais pas la solution malheureusement. La question est donc la que je suis en utilisant un serveur de dev url qui ne dispose pas d'une entrée de domaine sur le DNS mondial de serveur. Donc pour corriger cela, j'ai besoin de modifier le fichier hosts sur mon appareil Android/dans l'émulateur...personne ne sait comment cela peut être fait légitimement?

  • Selon la deuxième ligne du logcat: >07-16 16:32:32.907: W/Système.err(632): org.apache.http.conn.HttpHostConnectException: Connexion à devdashboard.société refusé. Êtes-vous sûr que ce n'est pas seulement un problème de réseau plutôt que d'un problème avec votre code? Avez-vous vérifié que la configuration du serveur?
  • Aussi êtes-vous sûr que ça devrait être un post?
  • aussi nous vous demandons de limiter ce que vous publiez. Vous devez modifier des informations spécifiques à votre environnement.
  • ping de l'url et l'adresse ip de votre connexion pour obtenir plus d'informations.
  • Thind, je suis en mesure d'accéder le site web via un navigateur et par le curling à partir de la ligne de commande donc je pense que ce n'est pas un problème de réseau, mais je suis ouvert aux suggestions
  • Avez-vous le faire à partir de l'appareil ou de l'émulateur? Autre que cela, je suis à court d'idées pour l'instant.
  • J'ai essayé les deux un Nexus S 4g et un émulateur et je reçois toujours la pendaison de problème, merci d'essayer! Laissez-moi savoir si quelque chose d'autre vient à vous
  • Je vais avoir le même message d'erreur, mais rarement, il m'arrive uniquement sur des tablettes, les téléphones portables, est de travailler à droite. Des idées?

InformationsquelleAutor VinC | 2012-07-16