这是java的源代码:
package com.example.entrepreneurexpress;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class InvestorRegister extends Activity {
EditText YourName;
EditText email;
EditText password;
EditText confirmPassword;
Button btnClear, btnRegister;
String nm, emailAdd, cnfPass, pwd, result = null;
int code;
InputStream is = null;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.investors_registration);
if(Build.VERSION.SDK_INT >= 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
ActionBar aBar = getActionBar();
aBar.setDisplayHomeAsUpEnabled(true);
YourName = (EditText) findViewById(R.id.invRegName);
email = (EditText) findViewById(R.id.invRegEmail);
password = (EditText) findViewById(R.id.invRegPassword);
confirmPassword = (EditText) findViewById(R.id.invRegConfPassword);
btnRegister = (Button) findViewById(R.id.btnInvRegRegister);
btnRegister.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(
(YourName.getText().toString().equals("")) &&
(email.getText().toString().equals("")) &&
(password.getText().toString().equals(""))
) {
Toast.makeText(getApplicationContext(), "Please Fill In All The Details", Toast.LENGTH_LONG).show();
} else {
nm = YourName.getText().toString();
emailAdd = email.getText().toString();
pwd = password.getText().toString();
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair(nm, email.getText().toString()));
nameValuePairs.add(new BasicNameValuePair(emailAdd, emailAdd));
nameValuePairs.add(new BasicNameValuePair(pwd, password.getText().toString()));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.bijukathemovie.com/insert.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("log_tag", "connection success ");
Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
} catch(Exception e) {
Log.e("log_tag", "Error in http connection "+e.toString());
Toast.makeText(getApplicationContext(), "Connection fail", Toast.LENGTH_SHORT).show();
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
} catch(Exception e) {
Log.e("log_tag", "Error converting result "+e.toString());
}
try {
JSONObject json_data = new JSONObject(result);
CharSequence w = (CharSequence) json_data.get("re");
Toast.makeText(getApplicationContext(), w, Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
这是PHP代码:
<?php
require_once("storescripts/dbconnect.php");
$name=$_POST['nm'];
$emailAdd=$_POST['emailAdd'];
$pwd=$_POST['pwd'];
$q="INSERT INTO Investors(invName, invEmail, invPassword) VALUES('".$name."', '".$emailAdd."', '".$pwd."')";
$s= mysqli_query($con, $q);
if(!$s) {
$r["re"]="Inserting problem in database";
print(json_encode($r));
} else {
$r["re"]="Record inserted successfully";
print(json_encode($r));
}
mysqli_close($con);
?>
您可以看到图片here
我正在关注this教程来完成我的工作。但是当我在我的设备上运行应用程序时,它工作正常,当在数据库中检查交叉时,我在数据库中获得NULL值。 NULL含义 - 数据库中没有输入。它是空白的。
请帮助我。感谢。
答案 0 :(得分:0)
这是因为你定义的nameValuePairs
nameValuePairs.add(new BasicNameValuePair("invName",
YourName.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("invEmail",
email.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("invPassword",
password.getText().toString()));
BasicNameValuePair
构造函数接受参数
name
作为查询参数的名称value
作为上述参数您提供的姓名为invName
,invEmail
和invPassword
。
但是在你的脚本中,你正在阅读查询参数
$name=$_POST['nm'];
$emailAdd=$_POST['emailAdd'];
$pwd=$_POST['pwd'];
由于nm
,emailAdd
和pwd
不是使用BasicNameValuePair
设置的名称,因此从_POST
读取NULL
会产生nameValuePairs.add(new BasicNameValuePair("nm",
YourName.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("emailAdd",
email.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("pwd",
password.getText().toString()));
。
并将相同的内容传递给数据库,因此您会看到插入的NULL数据。
在应用代码中进行如下更改:
$name=$_POST['invName'];
$emailAdd=$_POST['invEmail'];
$pwd=$_POST['invPassword'];
或更改脚本文件以将查询参数读取为:
{{1}}
然后运行你的程序。