HttpConnection:获得null响应

时间:2016-10-25 18:17:18

标签: android httpconnection

当我使用带参数的url命中GET服务时 http://example.com/API/SignUpAPI?Password=abc&MobileNo=123。 它给出状态200,响应是"成功"。

URL url=new URL(strings[0]);
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod(methodType);
        int responseCode = connection.getResponseCode();
        if(responseCode == HttpURLConnection.HTTP_OK){
            server_response = readStream(connection.getInputStream());
            Log.v("CatalogClient", server_response);
        }

但是

当我使用带参数的url命中GET服务时 http://example.com/API/SignUpAPI和使用OutputStreamWriter发送的参数。它给出状态200,响应为空。

 connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod(methodType);
        if(params!=null){
            String reqParam = "";
            StringBuilder buildParam = new StringBuilder();
            for (String paramKey : params.keySet()) {
                if (buildParam.length() > 0) {
                    buildParam.append("&");
                }
                buildParam.append(
                        URLEncoder.encode(paramKey, "UTF-8") + "=" + URLEncoder.encode(params.get(paramKey).toString(), "UTF-8"));
            }
            reqParam = buildParam.toString();
            connection.addRequestProperty("User-Agent","REST");
            System.setProperty("http.keepAlive", "false");
            connection.setRequestProperty("Accept", "*//*");
            connection.setConnectTimeout(10000);
            connection.setReadTimeout(10000);
            connection.setAllowUserInteraction(false);
            connection.setDoOutput(true);
            connection.connect();
            try (OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream())) {
                wr.write(reqParam);
            }
        }

        int responseCode = connection.getResponseCode();
        if(responseCode == HttpURLConnection.HTTP_OK){
            server_response = readStream(connection.getInputStream());
            Log.v("CatalogClient", server_response);
        }

由于

0 个答案:

没有答案