2015-07-13 56 views
0

這是我login.java 我要讓服務器註冊在Android如何在android中向服務器發送註冊數據?

public class Login extends Activity implements OnClickListener{ 
 

 
\t 
 
\t private EditText name,lastName,phoneNumber; 
 
\t private Button reg_btn; 
 
\t private ProgressDialog pDialog; 
 
\t JSONParser jsonParser = new JSONParser(); 
 
\t private static final String LOGIN_URL = "http://mojtabaapp.esy.es/register.php"; 
 
\t private static final String TAG_SUCCESS = "success"; 
 
\t private static final String TAG_MESSAGE = "message"; 
 
\t  
 
\t  
 
\t 
 
\t @Override 
 
\t protected void onCreate(Bundle savedInstanceState) { 
 
\t \t super.onCreate(savedInstanceState); 
 
\t \t setContentView(R.layout.activity_login); 
 
\t \t 
 
\t \t // tarif ha 
 
\t \t name=(EditText)findViewById(R.id.ET_name); 
 
\t \t lastName=(EditText)findViewById(R.id.ET_famili); 
 
\t \t phoneNumber=(EditText)findViewById(R.id.ET_phone); 
 
\t \t reg_btn=(Button)findViewById(R.id.reg_btn); 
 
\t \t 
 
\t \t reg_btn.setOnClickListener(this); 
 
\t \t 
 
\t \t 
 
\t } 
 
\t 
 
\t @Override 
 
\t public void onClick(View v) { 
 
\t \t // TODO Auto-generated method stub 
 
\t \t 
 
\t \t \t \t new CreateUser().execute(); 
 
\t \t 
 
\t } 
 
\t 
 
\t class CreateUser extends AsyncTask<String, String, String> { 
 
\t \t 
 
     boolean failure = false; 
 
\t \t 
 
     @Override 
 
     protected void onPreExecute() { 
 
      super.onPreExecute(); 
 
      pDialog = new ProgressDialog(Login.this); 
 
      pDialog.setMessage("..لطفا کمی صبر کنید"); 
 
      pDialog.setIndeterminate(false); 
 
      pDialog.setCancelable(true); 
 
      pDialog.show(); 
 
     } 
 
\t \t 
 
\t \t @Override 
 
\t \t protected String doInBackground(String... args){ 
 
\t \t \t 
 
\t \t \t // TODO Auto-generated method stub 
 
\t \t \t // Check for success tag 
 
      int success; 
 
      String NAME = name.getText().toString(); 
 
      String LASTNAME = lastName.getText().toString(); 
 
      
 
      String PHONE=phoneNumber.getText().toString(); 
 
      
 
      try { 
 
       // Building Parameters 
 
       List<NameValuePair> params = new ArrayList<NameValuePair>(); 
 
       params.add(new BasicNameValuePair("username", NAME)); 
 
       params.add(new BasicNameValuePair("password", LASTNAME)); 
 
       
 
       params.add(new BasicNameValuePair("phone", PHONE)); 
 

 
       Log.d("request!", "starting"); 
 
       
 
       //Posting user data to script 
 
       JSONObject json = jsonParser.makeHttpRequest(
 
         LOGIN_URL, "POST", params); 
 

 
       // full json response 
 
       Log.d("Login attempt", json.toString()); 
 

 
       // json success element 
 
       success = json.getInt(TAG_SUCCESS); 
 
       if (success == 1) { 
 
       \t Log.d("User Created!", json.toString());    \t 
 
       \t finish(); 
 
       \t return json.getString(TAG_MESSAGE); 
 
       }else{ 
 
       \t Log.d("Login Failure!", json.getString(TAG_MESSAGE)); 
 
       \t return json.getString(TAG_MESSAGE); 
 
       \t 
 
       } 
 
      } catch (JSONException e) { 
 
       e.printStackTrace(); 
 
      } 
 

 
      return null; 
 
\t 
 
\t \t \t 
 
\t \t } 
 
\t \t 
 
\t \t protected void onPostExecute(String file_url) { 
 
\t    // dismiss the dialog once product deleted 
 
\t    pDialog.dismiss(); 
 
\t    if (file_url != null){ 
 
\t    \t Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show(); 
 
\t    } 
 
\t 
 
\t   } 
 
} 
 

 
\t @Override 
 
\t public boolean onCreateOptionsMenu(Menu menu) { 
 
\t \t // Inflate the menu; this adds items to the action bar if it is present. 
 
\t \t getMenuInflater().inflate(R.menu.login, menu); 
 
\t \t return true; 
 
\t } 
 

 
\t @Override 
 
\t public boolean onOptionsItemSelected(MenuItem item) { 
 
\t \t // Handle action bar item clicks here. The action bar will 
 
\t \t // automatically handle clicks on the Home/Up button, so long 
 
\t \t // as you specify a parent activity in AndroidManifest.xml. 
 
\t \t int id = item.getItemId(); 
 
\t \t if (id == R.id.action_settings) { 
 
\t \t \t return true; 
 
\t \t } 
 
\t \t return super.onOptionsItemSelected(item); 
 
\t } 
 
}

這就是我的Java代碼

<?php 
 

 
require("config.inc.php"); 
 

 
if (!empty($_POST)) { 
 
     if (empty($_POST['username']) || empty($_POST['password']) || empty($_POST['phone'])) { 
 
     $response["success"] = 0; 
 
     $response["message"] = "error"; 
 
     die(json_encode($response)); 
 
    } 
 
    $query = " SELECT 1 FROM users WHERE username = :user"; 
 
    $query_params = array(
 
     ':user' => $_POST['username'] 
 
    ); 
 
    
 
    try { 
 
     
 
     $stmt = $db->prepare($query); 
 
     $result = $stmt->execute($query_params); 
 
    } 
 
    catch (PDOException $ex) { 
 

 
     $response["success"] = 0; 
 
     $response["message"] = "Error"; 
 
     die(json_encode($response)); 
 
    } 
 

 
    $row = $stmt->fetch(); 
 
    if ($row) { 
 

 
     $response["success"] = 0; 
 
     $response["message"] = "I'm sorry, this username is already in use"; 
 
     die(json_encode($response)); 
 
    } 
 

 
    $query = "INSERT INTO users (username, password , phone) VALUES (:user, :pass , :phonenumber) "; 
 

 
    $query_params = array(
 
     ':user' => $_POST['username'], 
 
     ':pass' => $_POST['password'], 
 
     ':phonenumber' => $_POST['phone'] 
 
    ); 
 
    
 
    //time to run our query, and create the user 
 
    try { 
 
     $stmt = $db->prepare($query); 
 
     $result = $stmt->execute($query_params); 
 
    } 
 
    catch (PDOException $ex) { 
 
     // For testing, you could use a die and message. 
 
     //die("Failed to run query: " . $ex->getMessage()); 
 
     
 
     //or just use this use this one: 
 
     $response["success"] = 0; 
 
     $response["message"] = "Database Error2. Please Try Again!"; 
 
     die(json_encode($response)); 
 
    } 
 

 
    $response["success"] = 1; 
 
    $response["message"] = "Username Successfully Added!"; 
 
    echo json_encode($response); 
 

 
} else { 
 
?> 
 
\t <h1>Register</h1> 
 
\t <form action="register.php" method="post"> 
 
\t  Username:<br /> 
 
\t  <input type="text" name="username" value="" /> 
 
\t  <br /><br /> 
 
\t  Password:<br /> 
 
\t  <input type="password" name="password" value="" /> 
 
\t  <br /><br /> 
 
\t  <input type="submit" value="Register New User" /> 
 
\t </form> 
 
\t <?php 
 
} 
 

 
?>

這是我的。主機上的PHP代碼

,但我的應用程序要forcestop這樣 請幫我 哪裏是我的問題

我對數據庫 用戶名密碼 ID 手機

+1

在您的doInBackground()方法中,您打電話給\t finish();我相當肯定會結束你的活動。如果需要幫助診斷問題,則需要發佈堆棧跟蹤。 –

回答

0

正如@Ivan Wooll說你調用finish()創建doInBackground()如果塊內

if (success == 1) { } 

這意味着您的數據使用PHP腳本成功保存,並且由於插入成功,狀態消息返回爲1,因此將調用finish()以關閉您的活動。

嘗試從該塊中刪除該段代碼。

希望有所幫助。

相關問題