我正在将我的数据字符串从Android应用程序发送到MySQL数据库,但是我收到来自服务器的响应消息" Server Response data string is empty[true,null]"
。我发送的数据存在于服务器端的日志文件(insert.php文件)中,但未插入数据库中
这是我的Android代码:
JSONObject obj = new JSONObject();
try {
obj.put("first_name", fname);
obj.put("last_name", lname);
obj.put("email", mail);
obj.put("phone", dv);
} catch(Exception ex) {
Log.e("Debug:Error 1", "json data error in contact create: " + ex.getMessage(), ex);
}
Log.d("My String", "string result :[" + obj + "]" + obj.length());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"contact\""+ lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(obj.toString());
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// close streams
Log.e("Debug","File is written");
dos.flush();
dos.close();
}
catch (MalformedURLException ex)
{
Log.e("Debug:Error 2", "error: " + ex.getMessage(), ex);
}
catch (IOException ioe)
{
Log.e("Debug:Error 3", "error: " + ioe.getMessage(), ioe);
}
//------------------ read the SERVER RESPONSE
try {
inStream = new DataInputStream ( conn.getInputStream() );
Log.d("inStream",""+inStream);
String str;
BufferedReader reader = new BufferedReader
(new InputStreamReader(inStream,"iso-8859-1"),8);
// while (( str = inStream.readLine())!=null)
while (( str = reader.readLine()) != null)
{
response = str;
Log.e("Response Msg","Server Response(Campaign_Create) "+ response);
}
inStream.close();
}
catch (IOException ioex){
Log.e("Debug :Error 5", "error: " + ioex.getMessage(), ioex);
}
// return response;
}
php code ..
<?php
include_once '../dbconnect.php';
class contact{
public function post(){
$json = file_get_contents('php://input');
$obj = json_decode($json, true);
error_log(date('Ymd H:i:s::').serialize($_POST)."\n",3,date('Ymd').".log");
echo exec("echo json: $json >> /tmp/check1.txt");
echo exec("echo obj: $obj >> /tmp/check1.txt");
if($obj != ""){
$id=$obj["contact_id"];
$last=$obj["last_name"];
$phone=$obj["phone"];
$email=$obj["email"];
echo exec("echo name: $name >> /tmp/check1.txt");
$result=mysql_query("insert into contact(contact_id,first_name,last_name ,phone,email) values('$id','$name','$last', '$phone')");
return $result;
}
else
{
echo "Data String is Empty.";
}
} //func
} //class
?>
答案 0 :(得分:0)
我不知道为什么上面的代码没有用,所以这里是我实现并正在运行的新代码。
String fname = firstName.getText().toString();
String lname = lastName.getText().toString();
String mail = email.getText().toString();
String dv = dropx.getSelectedItem().toString();
ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("first_name", fname));
params.add(new BasicNameValuePair("last_name", lname));
params.add(new BasicNameValuePair("email", mail));
params.add(new BasicNameValuePair("phone", dv));
try
{
HttpClient httpclients = new DefaultHttpClient();
HttpPost httpposts = new HttpPost(url);
httpposts.setEntity(new UrlEncodedFormEntity(params));
HttpResponse respons = httpclients.execute(httpposts);
HttpEntity entity = respons.getEntity();
isi = entity.getContent();
String msg = "Thank You for Signup.";
Toast.makeText(getApplicationContext(),msg, Toast.LENGTH_LONG).show();
Log.e("pass 11", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 11", e.toString());
Toast.makeText(getApplicationContext(), "Invalid IP Address",
Toast.LENGTH_LONG).show();
}
这是我的contact.php代码。
<?php
include_once '../dbconnect.php';
class contact{
public function post(){
$id=$_POST["first_name"];
$last=$_POST["last_name"];
$phone=$_POST ["phone"];
$email=$_POST ["email"];
$result=mysql_query("insert into contact(contact_id,first_name,last_name ,phone,email)
values('$id','$name','$last', '$phone')");
return $result;
} //func
} //class
?>