Android HttpURLConnection-返回用户请求后的响应

Android HttpURLConnection-返回用户请求后的响应,android,httpurlconnection,Android,Httpurlconnection,我有一个功能,注册一个表单到web服务器! 我这样称呼它: dialog = ProgressDialog.show(mActivity, "", getString(R.string.loading), true); new Thread(new Runnable() { public void run() { res = register(mobile, email, pass, name, family, gender); } }).start(

我有一个功能,注册一个表单到web服务器! 我这样称呼它:

dialog = ProgressDialog.show(mActivity, "", getString(R.string.loading), true);
new Thread(new Runnable() {
     public void run() {
           res = register(mobile, email, pass, name, family, gender);
      }
}).start();
问题在于,当用户第一次单击“注册”按钮时,它会显示“错误”,并且没有响应代码,但在第二次单击它的确定
这是我的代码:

private String register(String mobile, String email, String pass, String name, String family, int gender) {
    StringBuffer response = new StringBuffer();
    URL url;
    HttpURLConnection connection = null;
    String urlParameters =  null;
    try {
        urlParameters = USER_FIRST_NAME+"=" + URLEncoder.encode(name, "UTF-8") +
                USER_LAST_NAME + "&=" + URLEncoder.encode(family, "UTF-8") +
                USER_EMAIL + "&=" + URLEncoder.encode(email, "UTF-8") +
                USER_MOBILE + "&=" + URLEncoder.encode(mobile, "UTF-8") +
                USER_PASSWORD + "&=" + URLEncoder.encode(pass, "UTF-8") +
                USER_GENDER + "&=" + URLEncoder.encode(String.valueOf(gender), "UTF-8");



        //Create connection
        url = new URL(registerLink);
        connection = (HttpURLConnection)url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded");

        connection.setRequestProperty("Content-Length", "" +
                Integer.toString(urlParameters.getBytes().length));
        connection.setRequestProperty("Content-Language", "en-US");

        connection.setUseCaches (false);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        //Send request
        DataOutputStream wr = new DataOutputStream (
                connection.getOutputStream ());
        wr.writeBytes (urlParameters);
        wr.flush ();
        wr.close ();

        //Get Response
        int responceCode = connection.getResponseCode();
        if (responceCode == HttpURLConnection.HTTP_OK) {
            InputStream is = connection.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            String line;

            while((line = rd.readLine()) != null) {
                response.append(line);
                response.append('\r');
            }
            rd.close();
        }
        else response.append( "Error "+String.valueOf(responceCode));


    } catch (Exception e) {

        e.printStackTrace();
    } finally {
        dialog.dismiss();
        if(connection != null) {
            connection.disconnect();
        }
        return response.toString();
    }
}

注册按钮?我没有看到任何人。
USER\u LAST\u NAME+”&=“
?”??应该是
“&”+USER\u LAST\u NAME+“=”
@greenapps为什么需要这个按钮?我问题中的第一部分代码是在BTN的onClick listner中,您应该在之前告诉我。这个问题救了我。您在哪里取消/取消进度对话框?请对“&=”评论作出反应。