您好我创建了一个ANDROID应用程序,它将订单发送到网络服务器并将它们传递给数据库。
虽然在我的应用程序中我多次使用asynctask将json值传递给php文件,但现在我已经堆叠了两天。
我所做的是创建json值并调用URL将值传递给PHP文件。
下面是我发送json的'doInBackground'函数:
//I PASS THE json object which look like:
{
"app_id":"5",
"items":[{"orderItemPrice":5,"orderPrID":59,"orderItems":1}],
"serial":"cb734fea0c0d40e65",
"orderDate":"2014-06-22 09:41:21",
"orderComment":"my comment",
"orderCoupon":"1111"
}
///
protected Boolean doInBackground(JSONObject... obj) {
SchemeRegistry supportedSchemes = new SchemeRegistry();
SocketFactory sf = PlainSocketFactory.getSocketFactory();
supportedSchemes.register(new Scheme("http", sf, 80));
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
HttpProtocolParams.setUseExpectContinue(params, true);
ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, supportedSchemes);
DefaultHttpClient httpclient = new DefaultHttpClient(ccm, params);
try{
HttpPost httppost = new HttpPost(AppConstants.URL_SUBMIT_ORDERS);
httppost.setHeader("Content-type", "application/json");
StringEntity se = new StringEntity(obj.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(se);
HttpResponse response = httpclient.execute(httppost);
String temp = EntityUtils.toString(response.getEntity());
//THE RETURNED VALUE IS A MYSQL ERROR "COLUMN ID CANNOT BE NULL"
Log.i("tag", temp);
return true;
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch(Throwable t){ //new line at 07/04/2014
t.printStackTrace();// new line
}
return false;
}
WebServer上的php文件是:
<?php
**#db info**
define("HOST", "xxxx");
define("USER", "xxx");
define("PASSWORD", "xxx");
define("DATABASE", "xxx");
define("CAN_REGISTER", "any");
define("DEFAULT_ROLE", "member");
define("SECURE", FALSE);
**#read data from client**
$fp = fopen('php://input','r');
$rawData = stream_get_contents($fp);
function getRandomWord($len = 9) {
$word = array_merge( range('A', 'Z'));
shuffle($word);
return substr(implode($word), 0, $len);
}
#CREATE ORDER NUMBER
$orderNumber = getRandomWord();
try{
$arr = json_decode($rawData,true);//convert it to array
$arrCount = count($arr['items']);
#DB connection
$DBH = new PDO("mysql:host=xxxx;dbname=xxx",USER,PASSWORD);
$DBH->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
#step 1
#insert MAIN ORDER
$STH = $DBH->prepare("INSERT INTO tbl_orders (`orderfk_appID`,`orderfk_userSerial`,`orderNumber`,`orderCoupon`,`orderComment`,`orderDate`) values (:app_id,:serial,:orderNumber,:orderCoupon,:orderComment,:orderDate)");
$STH->bindParam(':app_id',$arr['app_id']);
$STH->bindParam(':serial',$arr['serial']);
$STH->bindParam(':orderNumber',$orderNumber);
$STH->bindParam(':orderCoupon',$arr['orderCoupon']);
$STH->bindParam(':orderComment',$arr['orderComment']);
$STH->bindParam(':orderDate',$arr['orderDate']);
#execute
$STH->execute();
$LastOrderId = $DBH->lastInsertId();
$DBH = null; //close the connection
#return result to the client
echo json_encode(array("Orders"=>$LastOrderId));
}
catch(PDOException $e){
echo "error: ".$e->getMessage();
file_put_contents('PDOErrors.txt', $e->getMessage()."<br/><br/>", FILE_APPEND);
}
&GT;
它接缝不会将json值传递给PHP文件,因为每当我尝试使用print_r($ arr)显示数组时;或echo json_encode(array(“Orders”=&gt; $ arr));我得到了空。
请有人帮助我并提及我没有看到的错误。谢谢。