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

当我用Google搜索时,请帮忙,但没有任何帮助

最佳答案

很明显,这不是路由器防火墙相关的问题,因为您位于同一网络中,因此只有几种可能性:


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


您应该确保可以以其他方式打开该服务,这将有助于您调试问题的出处。如果您已经完成此操作,建议您使用一些调试工具来跟踪TCP数据包(我不知道您在目标计算机上使用哪种操作系统;如果使用的是Linux发行版,则tcpdump可能会有所帮助)。

前提是您在AndroidManifest.xml文件中拥有android.permission.INTERNET权限。

10-05 18:05