duanshai4484 2015-09-27 07:17
浏览 24

HttpURLConnection发布问题

I have the following code in my program, used for inserting record -

private Void downloadUrl() throws IOException {
        String postParameters;
        HttpURLConnection urlConnection = null;

        postParameters = "name="+ URLEncoder.encode(user.name,"UTF-8")
                +"&age="+URLEncoder.encode(user.age+"","UTF-8")
                +"&username="+URLEncoder.encode(user.username,"UTF-8")
                +"&password="+URLEncoder.encode(user.password,"UTF-8");

        if (postParameters != null) {
            Log.i("Post", "POST parameters: " + postParameters);
            try {
                URL urlToRequest = new URL(SERVER_ADDRESS);
                urlConnection = (HttpURLConnection) urlToRequest.openConnection();
                urlConnection.setReadTimeout(30000 /* milliseconds */);
                urlConnection.setConnectTimeout(30000 /* milliseconds */);
                urlConnection.setDoOutput(true);
                urlConnection.setRequestMethod("POST");
                urlConnection.setRequestProperty("Content-Type",
                        "application/x-www-form-urlencoded");
                urlConnection.setFixedLengthStreamingMode(
                        postParameters.getBytes().length);
                PrintWriter out = new PrintWriter(urlConnection.getOutputStream());
                out.print(postParameters.getBytes());
                out.close();

                // handle issues
                int statusCode = urlConnection.getResponseCode();
                Log.e("Response", "The response is: " + statusCode);

            } catch (MalformedURLException e) {
                // handle invalid URL
                Log.e("Response", "invalid URL");
            } catch (SocketTimeoutException e) {
                // hadle timeout
                Log.e("Response", "handle timeout");
            } catch (IOException e) {
                // handle I/0
                Log.e("Response", "handle IO problem");
            } finally {
                if (urlConnection != null) {
                    urlConnection.disconnect();
                }
            }
        }
        return null;
    }

The code is showing no errors and the logged entries are

09-27 12:24:17.944 5006-5039/? E/Response﹕ The response is: 200 09-27 12:24:33.729 5006-5118/? I/Post﹕ POST parameters: name=vibha&age=25&username=vib&password=1234

But when I check the database, the new record is not created.

But if I use PHP code in a file and post the same parameters,the records are getting inserted.

My requirement for help and guidance is how to pass the post parameter in the HttpURLConnection class

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看