我正在使用Android Studio创建一个应用,该应用需要将来自足球队的球员姓名存储在sql数据库中。尽管在将用户注册为应用程序其他功能的一部分时能够执行此操作,但我无法将此数据发送到数据库。
有人可以帮助我吗?数据不会发布似乎很奇怪。
对于无法修复的代码格式,我深表歉意
这是backgroundWorker类中的代码:
public class BackgroundWorker extends AsyncTask<String,Void,String> {
Context context;
AlertDialog alertDialog;
/**
* Constructor
* @param ctx
*/
BackgroundWorker(Context ctx){
context=ctx;
}
@Override
protected String doInBackground(String... params) {
String type = params[0];
String login_url = "http://channan06.lampt.eeecs.qub.ac.uk/summer/login.php";
String register_url = "http://channan06.lampt.eeecs.qub.ac.uk/summer/register.php";
String player_url= "http://channan06.lampt.eeecs.qub.ac.uk/summer/addplayer.php";
if (type.equals("login")) {
try {
String user_name = params[1];
String password = params[2];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("user_name", "UTF-8") + "="
+ URLEncoder.encode(user_name, "UTF-8") + "&"
+ URLEncoder.encode("password", "UTF-8") + "="
+ URLEncoder.encode(password, "UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String result = "";
String line = "";
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else if (type.equals("addplayer")) {
try {
String name = params[1];
String telephone = params[2];
String email= params[3];
URL url = new URL(player_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("name", "UTF-8") + "="
+ URLEncoder.encode(name, "UTF-8") + "&"
+ URLEncoder.encode("telephone", "UTF-8") + "="
+ URLEncoder.encode(telephone, "UTF-8")+"&"
+ URLEncoder.encode(email,"UTF-8")+"="
+ URLEncoder.encode("email","UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String result = "";
String line = "";
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else if (type.equals("register")) {
try {
String name = params[1];
String surname = params[2];
String email = params[3];
String username = params[4];
String password = params[5];
URL url = new URL(register_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") + "&"
+ URLEncoder.encode("surname", "UTF-8") + "=" + URLEncoder.encode(surname, "UTF-8") + "&"
+ URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(email, "UTF-8") + "&"
+ URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8") + "&"
+ URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String result = "";
String line = "";
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPreExecute() {
alertDialog= new AlertDialog.Builder(context).create();
alertDialog.setTitle("Note");
}
@Override
protected void onPostExecute(String result) {
alertDialog.setMessage(result);
if(result.equals("Registration successful")){
Toast.makeText(context, "Registration success", Toast.LENGTH_SHORT).show();
Intent intent= new Intent(context, Home.class);
context.startActivity(intent);
} else if(result.equals("Login successful")){
Toast.makeText(context, "Login success", Toast.LENGTH_SHORT).show();
Intent intent= new Intent(context, Home.class);
context.startActivity(intent);
} else if(result.equals("login unsuccessful")) {
Toast.makeText(context, "Invalid username and password combination", Toast.LENGTH_SHORT).show();
}
else if(result.equals("New player added")){
Toast.makeText(context,"yeha",Toast.LENGTH_SHORT).show();
} else if(result.equals("Error")){
Toast.makeText(context,"fjuck",Toast.LENGTH_SHORT).show();
}
else{
alertDialog.show();
Toast.makeText(context, "Cannot have duplicate entries", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
这是Panel类中的代码:
public class Panel extends AppCompatActivity implements PlayerAdapter.itemClicked{
EditText etPlayerName,etPlayerTel,etPlayerEmail;
Button btnAddPlayer;
ListFrag listFrag;
FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_panel);
etPlayerName=(EditText)findViewById(R.id.etPlayerName);
etPlayerTel=(EditText)findViewById(R.id.etPlayerTel);
etPlayerEmail=(EditText)findViewById(R.id.etPlayerEmail);
btnAddPlayer=findViewById(R.id.btnPlayerAdd);
fragmentManager=this.getSupportFragmentManager();
listFrag=(ListFrag) fragmentManager.findFragmentById(R.id.fragment);
btnAddPlayer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(etPlayerName.getText().toString().isEmpty()||etPlayerTel.getText().toString().isEmpty()
|| etPlayerEmail.getText().toString().isEmpty()){
Toast.makeText(Panel.this,"Please enter all details",Toast.LENGTH_SHORT).show();
}else {
ApplicationClass.people.add(new Player(etPlayerName.getText().toString().trim(),
etPlayerTel.getText().toString().trim(),
etPlayerEmail.getText().toString().trim()));
Toast.makeText(Panel.this, "Player added to panel", Toast.LENGTH_SHORT).show();
etPlayerName.setText(null);
etPlayerTel.setText(null);
etPlayerEmail.setText(null);
listFrag.notifyDataChanged();
}
}
});
}
public void OnAddPlayer(View view){
String str_name=etPlayerName.getText().toString();
String str_telephone= etPlayerTel.getText().toString();
String str_email=etPlayerEmail.getText().toString();
String type= "addplayer";
BackgroundWorker backgroundWorker= new BackgroundWorker(this);
backgroundWorker.execute(type,str_name,str_telephone,str_email);
}
@Override
public void onItemClicked(int index) {
etPlayerName.setText(ApplicationClass.people.get(index).getName());
}
}
这与寄存器类中的代码非常相似,可以正常工作:
public class Register extends AppCompatActivity {
EditText etName,etSurname, etEmail,etUsername,etPassword,etReenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
etName=(EditText) findViewById(R.id.etName);
etSurname=(EditText) findViewById(R.id.etSurname);
etEmail =(EditText) findViewById(R.id.etEmail);
etUsername=(EditText) findViewById(R.id.etUsername);
etPassword=(EditText) findViewById(R.id.etPassword);
etReenter=(EditText) findViewById(R.id.etReenter);
}
public void OnReg(View view){
if(etName.getText().toString().isEmpty()||etSurname.getText().toString().isEmpty()
|| etEmail.getText().toString().isEmpty()||etUsername.getText().toString().isEmpty()
|| etPassword.getText().toString().isEmpty()||etReenter.getText().toString().isEmpty()){
Toast.makeText(Register.this,"Please enter all details",Toast.LENGTH_SHORT).show();
} else {
if(etPassword.getText().toString().trim().equals(etReenter.getText().toString().trim())==false){
Toast.makeText(Register.this,"Please ensure passwords match",Toast.LENGTH_SHORT).show();
}
else {
String str_name = etName.getText().toString();
String str_surname = etSurname.getText().toString();
String str_email = etEmail.getText().toString();
String str_username = etUsername.getText().toString();
String str_password = etPassword.getText().toString();
String type = "register";
BackgroundWorker backgroundWorker = new BackgroundWorker(this);
backgroundWorker.execute(type, str_name, str_surname, str_email, str_username, str_password);
}
}
}
}