2015-12-30 95 views
1

我需要創建一個具有註冊活動的Android應用程序,所以我需要將一些數據插入到mysql數據庫中。我嘗試了一些不同的教程和指南,但沒有任何幫助我解決我的問題。從Android插入數據到MySql中

誰能幫幫我?

我後我的最後代碼在這裏:

Register.java

public class Register extends AppCompatActivity implements View.OnClickListener { 

private Button bRegister; 
private EditText etName, etSurname, etAge, etEmail, etPassword; 

private static final String REGISTER_URL = "http://localhost/sFitness/Register.php"; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_register); 

    etName = (EditText) findViewById(R.id.etName); 
    etSurname = (EditText) findViewById(R.id.etSurname); 
    etAge = (EditText) findViewById(R.id.etAge); 
    etEmail = (EditText) findViewById(R.id.etEmail); 
    etPassword = (EditText) findViewById(R.id.etPassword); 

    bRegister = (Button) findViewById(R.id.bRegister); 
    bRegister.setOnClickListener(this); 
} 

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
     case R.id.bRegister: 
      registerUser(); 
      break; 
    } 
} 

private void registerUser() { 
    String name = etName.getText().toString(); 
    String surname = etSurname.getText().toString(); 
    String age = etAge.getText().toString(); 
    String email = etEmail.getText().toString(); 
    String password = etPassword.getText().toString(); 

    register(name, surname, age, email, password); 
} 

private void register(String name, String surname, String age, String email, String password) { 
    class RegisterUser extends AsyncTask<String, Void, String> { 

     ProgressDialog loading; 
     RegisterUserClass ruc = new RegisterUserClass(); 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 

      loading = ProgressDialog.show(Register.this, "Please wait...", null, true, true); 
     } 

     @Override 
     protected void onPostExecute(String s) { 
      super.onPostExecute(s); 
      loading.dismiss(); 
      Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show(); 
     } 

     @Override 
     protected String doInBackground(String... params) { 
      HashMap<String, String> data = new HashMap<String, String>(); 
      data.put("name", params[0]); 
      data.put("surname", params[1]); 
      data.put("age", params[2]); 
      data.put("email", params[3]); 
      data.put("password", params[4]); 

      String result = ruc.sendPostRequest(REGISTER_URL, data); 
      return result; 
     } 
    } 
    RegisterUser ru = new RegisterUser(); 
    ru.execute(name, surname, age, email, password); 
} 
} 

RegisterUserClass.java

import org.apache.http.params.HttpParams; 
import org.apache.http.*; 

import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.InputStreamReader; 
import java.io.OutputStream; 
import java.io.OutputStreamWriter; 
import java.io.UnsupportedEncodingException; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.net.URLEncoder; 
import java.util.HashMap; 
import java.util.Map; 

import javax.net.ssl.HttpsURLConnection; 

/** 
* Created by Silvia Berardo on 29/12/2015. 
*/ 



public class RegisterUserClass { 

public String sendPostRequest(String requestURL, 
           HashMap<String, String> postDataParams) { 

    URL url; 
    String response = ""; 
    try { 
     url = new URL(requestURL); 

     HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
     conn.setReadTimeout(15000); 
     conn.setConnectTimeout(15000); 
     conn.setRequestMethod("POST"); 
     conn.setDoInput(true); 
     conn.setDoOutput(true); 


     OutputStream os = conn.getOutputStream(); 
     BufferedWriter writer = new BufferedWriter(
       new OutputStreamWriter(os, "UTF-8")); 
     writer.write(getPostDataString(postDataParams)); 

     writer.flush(); 
     writer.close(); 
     os.close(); 
     int responseCode=conn.getResponseCode(); 

     if (responseCode == HttpsURLConnection.HTTP_OK) { 
      BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream())); 
      response = br.readLine(); 
     } 
     else { 
      response="Error Registering"; 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    return response; 
} 

private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException { 
    StringBuilder result = new StringBuilder(); 
    boolean first = true; 
    for(Map.Entry<String, String> entry : params.entrySet()){ 
     if (first) 
      first = false; 
     else 
      result.append("&"); 

     result.append(URLEncoder.encode(entry.getKey(), "UTF-8")); 
     result.append("="); 
     result.append(URLEncoder.encode(entry.getValue(), "UTF-8")); 
    } 

    return result.toString(); 
} 
} 

的問題是,這個代碼給我發郵件,告訴我該數據庫不起作用...爲什麼?

+0

什麼是你的錯誤消息回來? – apmartin1991

+0

當我運行應用程序時,點擊註冊按鈕後,成功或失敗的消息沒有顯示,並進入數據庫沒有任何東西。進入控制檯:'E/Surface:getSlotFromBufferLocked:unknown buffer:0xb4053810'' W/System.err:java.net.ConnectException:在15000ms後無法連接到localhost/127.0.0.1(端口80):isConnected失敗:ECONNREFUSED拒絕)''E /表面:getSlotFromBufferLocked:未知的緩衝區:0xb4057860''E /表面:getSlotFromBufferLocked:未知的緩衝區:0xb40578d0' @ apmartin1991 –

+0

我假設你在你的android模擬器上得到這個錯誤是的? – apmartin1991

回答

0

更換行:

private static final String REGISTER_URL = "http://localhost/sFitness/Register.php"; 

隨着

private static final String REGISTER_URL = `"http://10.0.2.2/sFitness/Register.php";` 

本地主機並不是指你的PC的IP地址,但它指的是在你的模擬器本地主機。如果您使用10.0.2.2,那麼它應該工作或提出另一個錯誤消息。

如果你正在使用GenyMotion那麼你需要使用:

private static final String REGISTER_URL = `"http://10.0.3.2/sFitness/Register.php";` 

所有這些信息都被發現here

+0

請將答案標爲正確:) – apmartin1991