我需要创建一个带有注册活动的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 :(得分: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";`
localhost不是指您的PC IP地址,但它指的是您的模拟器上的localhost。如果您使用10.0.2.2,那么它应该工作或显示另一条错误消息。
如果您使用的是GenyMotion,则需要使用:
private static final String REGISTER_URL = `"http://10.0.3.2/sFitness/Register.php";`
找到所有这些信息here