我正在尝试使用应用程序在线数据库中输入用户数据,但数据未插入数据库中。 Php文件是正确的它是插入数据但我的应用程序不在这里是我的register.java文件代码。 包net.netai.hackt_swg.microbok;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
public class register extends AppCompatActivity {
EditText e_name,e_ml,e_ps;
String name,email,pas;
@Override
protected void onCreate(Bundle savedInstanceState) {
Button reg;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
reg=(Button)findViewById(R.id.rgs);
e_name=(EditText)findViewById(R.id.name);
e_ml=(EditText)findViewById(R.id.mls);
e_ps=(EditText)findViewById(R.id.pss);
reg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
rg();
}
});
}
public void rg(){
name=e_name.getText().toString();
email=e_ml.getText().toString();
pas=e_ps.getText().toString();
BackgroundTask backgroundTask=new BackgroundTask();
backgroundTask.execute(name,email,pas);
finish();
}
class BackgroundTask extends AsyncTask<String,Void ,String> {
String reg_URL;
@Override
protected void onPreExecute() {
reg_URL="http://hackt_swg.netai.net/app/register.php";
}
@Override
protected String doInBackground(String... params) {
String nm=params[0];
String em=params[1];
String ps=params[2];
try {
URL url=new URL(reg_URL);
HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
OutputStream outputStream=httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
String data= URLEncoder.encode("name","UTF-8")+"="+URLEncoder.encode(nm,"UTF-8")+"&"+
URLEncoder.encode("email","UTF-8")+"="+URLEncoder.encode(em,"UTF-8")+"&"+
URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(ps,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream in=httpURLConnection.getInputStream();
in.close();
httpURLConnection.disconnect();
return "registration successful";
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "null";
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG).show();
}
}
}
我的MainActivity.java是
package net.netai.hackt_swg.microbok;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
Button lgn,sgnup;
EditText e_password,e_email;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lgn=(Button)findViewById(R.id.loginbtn);
sgnup=(Button)findViewById(R.id.signupbtn);
e_email=(EditText)findViewById(R.id.mail);
e_password=(EditText)findViewById(R.id.pass);
ConnectivityManager connectivityManager= (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo=connectivityManager.getActiveNetworkInfo();
if(networkInfo!=null && networkInfo.isConnected())
{
}
else{
lgn.setEnabled(false);
sgnup.setEnabled(false);
e_password.setEnabled(false);
e_email.setEnabled(false);
}
lgn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
sgnup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
p();
}
});
}
public void p(){
Intent i=new Intent( this ,register.class);
startActivity(i);
}
}
register.php
<?php
$hostname="mysql13.000webhost.com";
$mysqlusername="a1544527_danzen";
$mysqlpassword="********";
$databasename = "a1544527_appData";
$db=mysqli_connect($hostname,$mysqlusername,$mysqlpassword,$databasename) or die("noooooo");
$fname=$_POST['name'];
$email=$_POST['email'];
$password=$_POST['password'];
mysqli_query($db,"INSERT INTO user (name,password,email) VALUES(
'". mysqli_escape_string($db,$fname) ."',
'". mysqli_escape_string($db,md5($password)) ."',
'". mysqli_escape_string($db,$email) ."') ") or die(mysql_error());
echo 'successfully registered';
?>
答案 0 :(得分:0)
您在项目中使用此代码,请准备好Okhttp lib将数据发送到服务器。当您使用此代码时,请调用此方法然后调用此方法但要小心。
public void Register(String nm, String em, String pm)
{
OkHttpClient client = new OkHttpClient();
RequestBody formBody = new FormBody.Builder()
.add("name", nm)
.add("email", em)
.add("password", pm)
.build();
Request request = new Request.Builder()
.url("http://hackt_swg.netai.net/app/register.php")
.post(formBody)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e)
{
Log.e(TAG, "Get Api Credentials Request Failed " + call.toString());
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException
{
String responseUser = response.body().string().toString();
Lor.i("[Registration]", ""+responseUser);
}
});
}
答案 1 :(得分:0)
我想你错过了网址。检查你的网址:http://hackt_swg.netai.net/app/register.php我也推荐使用Retrofit。这是最好的网络服务方式