org.apache.http.NoHttpResponseException: Le serveur cible n'a pas pu répondre
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpProtocolParams;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
private final String TAG = "ACCESS";
//constructor
public JSONParser() {
}
//function get json from url
//by making HTTP POST or GET mehtod
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
//Making HTTP request
try {
//check for request method
if(method.equals("POST")){
//request method is POST
//defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method.equals("GET")){
//request method is GET
Log.e(TAG, "check1");
DefaultHttpClient httpClient = new DefaultHttpClient();
Log.e(TAG, "check2");
HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), false);
Log.e(TAG, "check3");
String paramString = URLEncodedUtils.format(params, "utf-8");
Log.e(TAG, "check4");
url += "?" + paramString;
Log.e(TAG, "check5");
HttpGet httpGet = new HttpGet(url);
Log.e(TAG, "check6");
HttpResponse httpResponse = httpClient.execute(httpGet); //getting exception here
Log.e(TAG, "check7");
HttpEntity httpEntity = httpResponse.getEntity();
Log.e(TAG, "check8");
is = httpEntity.getContent();
Log.e(TAG, "check9");
}
}
catch (UnsupportedEncodingException e) {
Log.e(TAG, "e1"+e.toString());
return null;
}
catch (ClientProtocolException e) {
Log.e(TAG, "e2"+e.toString());
return null;
}
catch (IOException e) {
Log.e(TAG, "e3"+e.toString());//this exception occurs
return null;
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e(TAG, "e4"+" Error converting result " + e.toString());
return null;
}
//try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e(TAG, "e5"+" Error parsing data " + e.toString());
return null;
}
//return JSON String
return jObj;
}
}
Quand je suis appelant le code ci-dessus avec un objet jParser de classe ci-dessus, il donne exception.
private final String GET_ALL_MODEL_NAME = "http://10.0.0.2/android_connect/get_all_model_name.php";
List<NameValuePair> params = new ArrayList<NameValuePair>();
//getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(GET_ALL_MODEL_NAME, "GET", params);
Je suis l'exception. mon logcat est
02-23 03:52:55.702: E/ACCÈS(1603): check1
02-23 03:52:55.732: E/ACCÈS(1603): check2
02-23 03:52:55.732: E/ACCÈS(1603): check3
02-23 03:52:55.742: E/ACCÈS(1603): check4
02-23 03:52:55.742: E/ACCÈS(1603): check5
02-23 03:52:55.792: E/ACCÈS(1603): check6
02-23 03:56:22.442: E/ACCÈS(1603): e3org.apache.http.NoHttpResponseException: Le serveur cible n'a pas pu répondre
S'il vous plaît aider, comme je l'ai googlé mais ne trouvez rien d'utile
- Veuillez vérifier ce post, il vous aide. stackoverflow.com/questions/24970431/...
Vous devez vous connecter pour publier un commentaire.
Il est évident que c'est pas un routeur-firewall problème, vous êtes sous le même net, donc il n'y a que peu de possibilités:
Vous devriez assurez-vous que vous pouvez ouvrir ce service de quelque autre façon, qui pourrait vous aider débogage où le coupable est. Si vous avez déjà fait cela, je te suggère en utilisant certains outil de débogage pour tracer les paquets TCP (je ne sais pas non plus quel type de système d'exploitation que vous utilisez sur l'ordinateur de destination; si c'est une distribution linux,
tcpdump
peut aider).Tous que en supposant que vous avez le
android.permission.INTERNET
autorisation de votre AndroidManifest.xml fichier.