Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从WCF获取JSON HTTP POST请求_Android_Rest - Fatal编程技术网

Android 从WCF获取JSON HTTP POST请求

Android 从WCF获取JSON HTTP POST请求,android,rest,Android,Rest,我编写了一个简单的WCF Web服务来响应客户端。 在android上的简单应用程序中,我编写以下代码来连接我的web服务: private class SetUserFromServerToRv extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... params) { HttpURLConnection co

我编写了一个简单的WCF Web服务来响应客户端。 在android上的简单应用程序中,我编写以下代码来连接我的web服务:

    private class SetUserFromServerToRv extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        HttpURLConnection connection = null;
        String line = "";

        try {
            StringBuilder builder = new StringBuilder();

            URL url = new URL("http://localhost/WcfService_myChat/Service1.svc/json/AllUserQuery");
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.setRequestMethod("POST");
            connection.setUseCaches(false);
            connection.setConnectTimeout(10000);
            connection.setReadTimeout(150000);
            connection.setRequestProperty("Content-Type", "application/json");
            connection.connect();

            InputStream response = connection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(response,"utf-8"));
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
            connection.disconnect();
            reader.close();


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return line;
    }
}
我试过这个:

                URL url = new URL("http://10.0.2.2:8080/WcfService_myChat/Service1.svc/json/AllUserQuery");
但我还是有错误

通过邮递员我得到了正确的答案

我在手机上测试我的代码。我的错误在哪里?thnks

*********编辑**************

我尝试使用我的本地IP:

URL url = new URL("http://192.168.x.x/WcfService_myChat/Service1.svc/json/AllUserQuery");
但我犯了这个错误:

java.net.SocketTimeoutException: failed to connect to /192.168.100.19 (port 80) after 10000ms
如果您的系统和服务器位于本地LAN中,那么它将正常工作,因为 它将找到该服务器,但当您尝试从 无法找到移动数据,因为您正在使用移动数据进行搜索 此IP位于WWW中,但您的服务器IP是本地的,因此您必须连接您的 设备与您的本地局域网WI-FI连接,以便在移动设备中获得响应


@NMATUR我发现:10.0.2.2版本的服务器响应时间太长。您使用的是哪台服务器?@AbdulKawee我的服务器在我的本地。它是wamp还是xamp?可能是它们没有运行,或者它们的服务器存在问题configuration@AbdulKawee使用Vs(Visual studio)时并编写一个WSFWeb服务。VS本身用于IIS express。但我认为我的问题与安卓系统有关,因为邮递员得到了正确的答案如果你找到了正确的答案,那么你可以把它标记为正确的标志,它也会帮助其他用户得到正确的答案。
URL url = new URL("http://192.168.x.x/WcfService_myChat/Service1.svc/json/AllUserQuery");
java.net.SocketTimeoutException: failed to connect to /192.168.100.19 (port 80) after 10000ms