org.apache.http.NoHttpResponseException:目标服务器无法响应

时间:2014-02-23 09:30:26

标签: android

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;

  }
}

当我用上面一个类的对象jParser调用上面的代码时,它会给出异常。

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是

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:目标服务器无法响应

请帮助,因为我用谷歌搜索但没有找到任何帮助

1 个答案:

答案 0 :(得分:1)

很明显,这不是路由器与防火墙相关的问题,因为你在同一个网络下,所以只有几个可能性:

  • 在服务器端的那个IP上没有监听该端口
  • 服务器端有一个阻止连接尝试的本地防火墙
  • 您没有使用WIFI,因此您不在同一个网络下。

您应确保以某种方式打开该服务,这将有助于您调试罪魁祸首的位置。如果你已经这样做了,我建议使用一些调试工具来跟踪TCP数据包(我不知道你在目标机器上使用什么样的操作系统;如果它是一些linux发行版,tcpdump可能有帮助。)

假设您在AndroidManifest.xml文件中拥有android.permission.INTERNET权限。