【发布时间】:2014-12-11 17:37:40
【问题描述】:
我正在将我的数据字符串从 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 代码..
<?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
?>
【问题讨论】:
-
Im sending my data string from an Android app to a MySQL database。不,您的应用程序将其发送到 php 脚本。Data which I sent is present in my log file on server side (insert.php file) but not inserting in database.。那么PHP代码有问题。您没有显示您的 php 文件,因此我们无法提供帮助。Log.e("Debug","File is written");为什么要调用一些json文本文件? -
php 代码在这里。 “文件已写入”只是一条消息。
-
这不是一个令人满意的答案。请回答更多重点。
I get the response message from server saying "file is written. Server Response data string is empty[true,null]"。您的 php 脚本中没有任何内容在回显此类消息。请澄清。 -
文件被写入代码中的消息是在 dos.flush() 之前;表示数据已成功从应用程序发送到 php 文件。现在我收到文件的消息没有写入来自我的 php 文件的其他条件,这意味着空数据字符串。
-
“文件已写入”是您的 Android 客户端记录的内容。为什么说是从服务器接收的呢?
标签: php android web-services