org.apache.http.NoHtt presponseException:目标服务器无法响应 [英] org.apache.http.NoHttpResponseException: The target server failed to respond
问题描述
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;
}
}
当我打电话上述code以上的类的对象jParser,它提供了异常。
When I am calling the above code with an object jParser of above class, it gives 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);
我收到例外。我logcat的是
I am getting the exception. my logcat is
3月2日至23日:52:55.702:E / ACCESS(1603):CHECK1
3月2日至23日:52:55.732:E / ACCESS(1603):CHECK2
3月2日至23日:52:55.732:E / ACCESS(1603):check3
3月2日至23日:52:55.742:E / ACCESS(1603):check4
3月2日至23日:52:55.742:E / ACCESS(1603):check5
3月2日至23日:52:55.792:E / ACCESS(1603):check6
3月2日至23日:56:22.442:E / ACCESS(1603):e3org.apache.http.NoHtt presponseException:目标服务器无法响应
02-23 03:52:55.702: E/ACCESS(1603): check1
02-23 03:52:55.732: E/ACCESS(1603): check2
02-23 03:52:55.732: E/ACCESS(1603): check3
02-23 03:52:55.742: E/ACCESS(1603): check4
02-23 03:52:55.742: E/ACCESS(1603): check5
02-23 03:52:55.792: E/ACCESS(1603): check6
02-23 03:56:22.442: E/ACCESS(1603): e3org.apache.http.NoHttpResponseException: The target server failed to respond
请帮助,因为我用Google搜索,但没有发现任何有用的。
Please help, as I googled but find nothing helpful
推荐答案
很明显它不是一个路由器,防火墙,你是一样的网下相关的问题,所以只有几种可能性:
It's obvious it's not a router-firewall related problem as you are under the same net, so there are only a few possibilities:
- 没有什么对服务器端的IP侦听该端口
- 有上阻止该连接尝试服务器端的本地防火墙
- ,所以你在同一网下不是你不使用WIFI。
您应该确保您可以打开该服务的一些疗法的方式,这将有助于你在哪里调试的罪魁祸首是。如果你已经这样做了,我建议使用一些调试工具跟踪TCP数据包(我不知道你在目标计算机上使用或者什么样的操作系统;如果是一些Linux发行版, tcpdump的
可能会帮助)。
You should make sure you can open that service some ther way, that would help you debugging where the culprit is. If you've already done this, I'd suggest using some debugging tool to trace TCP packets (I don't know either what kind of operating system you use on the destination machine; if it's some linux distribution, tcpdump
might help).
所有这一切假设你在你的AndroidManifest.xml文件中的 android.permission.INTERNET对
的权限。
All that assuming you have the android.permission.INTERNET
permission in your AndroidManifest.xml file.
这篇关于org.apache.http.NoHtt presponseException:目标服务器无法响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!