2016-04-29 164 views
1

我的問題是如果我輸入用戶名,密碼,電子郵件註冊失敗的吐司消息出現。任何一個在背景任務類和主要活動中解決這個問題?Webservice中的問題

請問任何人都可以進入它看起來像註冊成功?

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(); 
     } 
    } 
    } 
+0

請檢查您的反應是正確的或者也不在郵遞員或hurlit。 – Lampard

回答

0

您要爲POST request.So你必須發送parameters.First在郵遞員檢查,如果該URL的響應正確與否的請求發送數據,之後,在你的代碼,它集成:

你在後臺方法要做的應該是這樣的:

@Override 
      protected String doInBackground(String... args) 
      { 
URL ur = new URL(reg_url); 
       HttpURLConnection httpURLConnection = (HttpURLConnection) ur.openConnection(); 
       OutputStream outputStream = httpURLConnection.getOutputStream(); 
       httpURLConnection.setRequestMethod("POST"); 
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
     nameValuePairs.add(new BasicNameValuePair("name, "Anil Kumar John")); 
       nameValuePairs.add(new BasicNameValuePair("password", "anil123")); 
     nameValuePairs.add(new BasicNameValuePair("email", "[email protected]")); 
    } 
+0

可以用http url連接寫@crazyandroid – AnilkumarJohn

+0

沒有你@ Anil Kumar John,你想要註冊的網址嗎? – Lampard

+0

是的,我想註冊的網址@瘋狂Android – AnilkumarJohn