我正在使用android studio从用户那里获取信息并将其插入到外部数据库中,我在youtube上看到很多视频,但在运行时仍然存在问题:“注册成功”但不插入进入DB
这是我在android studio中的代码
公共类注册扩展了AppCompatActivity {
EditText email,password,name;
Switch visually_impaired;
String Email,Password,Name,Visually_impaired,Beacon_alert,Buildings_alert;;
Context ctx = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
name = (EditText) findViewById(R.id.name_editText);
password = (EditText) findViewById(R.id.password_editText);
email = (EditText) findViewById(R.id.email_editText);
visually_impaired = (Switch) findViewById(R.id.blindOpt);
}
public void register(View v){
Email = email.getText().toString();
Name = name.getText().toString();
Password = password.getText().toString();
Visually_impaired = visually_impaired.getText().toString();
BackGround b = new BackGround(this);
b.execute(Email,Password,Name,Visually_impaired);
}
class BackGround extends AsyncTask<String,String,String> {
Context ctx;
public BackGround(Context ctx){
this.ctx = ctx;
}
@Override
protected String doInBackground(String... strings) {
String email = strings[0];
String password = strings[1];
String name = strings[2];
String visually_impaired = strings[3];
try {
URL url = new URL("http://ksuexpress.com/register.php");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
OutputStream os = httpURLConnection.getOutputStream();
BufferedWriter bufferWriter = new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
String data = URLEncoder.encode("email","UTF-8")+ " = "+URLEncoder.encode(email,"UTF-8") +"&"+
URLEncoder.encode("password","UTF-8")+ " = "+URLEncoder.encode(password,"UTF-8") +"&"+
URLEncoder.encode("name","UTF-8")+ " = "+URLEncoder.encode(name,"UTF-8") +"&"+
URLEncoder.encode("visually_impaired","UTF-8")+ " = "+URLEncoder.encode(visually_impaired,"UTF-8");
bufferWriter.write(data);
bufferWriter.flush();
bufferWriter.close();
os.close();
InputStream is = httpURLConnection.getInputStream();
is.close();
return "Registration success....";
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result){
Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
}
}
}
并且我在服务器中有一个使用POST并将其插入数据库的PHP代码
任何人都可以帮助我...
答案 0 :(得分:0)
一些可能的修复:
" = "
更改为"="
,不含空格doInBackground
中添加一些日志,并确保您在电子邮件,密码等中获取正确的参数。InputStream is = httpURLConnection.getInputStream(); is.close();
更改为httpURLConnection.connect();