Web服务中的问题

时间:2016-04-29 13:31:55

标签: java android web-services

我的问题是,如果我输入用户名,密码,电子邮件注册失败Toast消息,那么任何人都可以在后台任务类和主要活动中解决这个问题吗?

请任何一个人进入它看起来像注册成功吗?

 public void submitDetails(View v) 

{




        String username = et.getText().toString();
        String password = et1.getText().toString();
        String email = et2.getText().toString();

        String emailpatern = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
                + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
        if (username.equals("")) {
            et.setError("pls enter name");
        } else if (password.equals("")) {
            et1.setError("pls enter last name");
        } else if (email.equals("")) {
            et2.setError("Pls Enter Valid Email");
        } else if (!email.matches(emailpatern)) {
            et2.setError("Pls Enter Valid email charcters");
        }
        BackgroundTask backgroundTask = new BackgroundTask();
        backgroundTask.execute(username,password,email);

    }
    class BackgroundTask extends AsyncTask<String,Void,String>
    {
        String reg_url;
        @Override
        protected void onPreExecute() {
            reg_url ="http://iwt.devxenorix.com/webservices/create-user.php";
        }

        @Override
        protected String doInBackground(String... args)
        {
            String username,password,email;
            username = args[0];
            password = args[1];
            email = args[2];
            try {
                URL ur = new URL(reg_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection) ur.openConnection();
                OutputStream outputStream = httpURLConnection.getOutputStream();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                BufferedWriter bufferedWriter  = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
                String register = URLEncoder.encode("username","UTF-8")+"="+URLEncoder.encode(username,"UTF-8")+"&"+
                        URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8")+"&"+
                        URLEncoder.encode("email","UTF-8")+"="+URLEncoder.encode(email,"UTF-8");
                bufferedWriter.write(register);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();
                InputStream inputStream = httpURLConnection.getInputStream();
                inputStream.close();
                httpURLConnection.disconnect();
                return "Registered...";
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return "register failure";
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }

        @Override
        protected void onPostExecute(String result) {
            Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
        }
    }
    }

1 个答案:

答案 0 :(得分:0)

您正在发送POST请求的数据。因此,您必须发送参数。首先在邮递员中检查网址的响应是否正确,然后将其集成到您的代码中:

你在背景方法中应该是这样的:

L