我在从 yii 框架服务器获取价值时遇到了问题。从 yii 开发人员方面,他说我你什么都没有给我任何输入数据,但我已经准备好提供 xml 格式的数据。他说,我被认为是无效的。
YII 开发人员对我说:他的输入为空
$input = $GLOBALS['HTTP_RAW_POST_DATA'];
为什么输入为空,你们对这个问题有什么想法吗?
我的安卓端:
String url="http://test.org/site/test";
String input = String.format("<Es_Request><Data><UserName>%s</UserName><Password>%s</Password></Data></Es_Request>",userEmail, userPassword);
String response = HttpPostRetreiver.retriver(url,input, LoginScreen.this);
Log.d("TAG",LoginScreen.java--- 'New response is------->>>' Line 246===" + response);
和HttpPostRetreiver.java 文件
public class HttpPostRetreiver {
public static String retriver(String Url, String input, Context context) {
Log.d("TESTING", "URL->>" + Url);
String responseString = null;
StringEntity stringEntity;
HttpPost postRequest = new HttpPost(Url);
try {
Log.e("string is", input + "\n" + Url);
stringEntity = new StringEntity(input, "UTF-8");
stringEntity.setContentType("application/atom+xml");
postRequest.setEntity(stringEntity);
//postRequest.a
Log.v("Post", "Posted");
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(postRequest);
HttpEntity getResponseEntity = response.getEntity();
responseString = EntityUtils.toString(getResponseEntity);
} catch (Exception e) {
postRequest.abort();
Log.w("HttpPostRetreiver", "Error for URL " + Url, e);
}
return responseString;
}
}
在这里,我得到这样的回应:
08-10 17:42:52.581: D/TAG(24295): LoginScreen.java--- 'New response is------->>>' Line 246===<?xml version="1.0" encoding="utf-8"?><Es_Response><Bpn_Id> </Bpn_Id><Es_Session_Id> </Es_Session_Id><UserName></UserName><Password>[]</Password></Es_Response>
从 YII 开发者方面:
他是这样做的:
<?php
class UserapiController extends Controller
{
// Members
/**
* Key which has to be in HTTP USERNAME and PASSWORD headers
*/
Const APPLICATION_ID = 'ASCCPE';
/**
* Default response format
* either 'json' or 'xml'
*/
private $format = 'xml';
/**
* @return array action filters
*/
public function filters()
{
return array();
}
// Actions
public function actionList()
{
//print_r($_POST);exit;
if (isset($GLOBALS['HTTP_RAW_POST_DATA']) && !empty($GLOBALS['HTTP_RAW_POST_DATA'])) {
$input = $GLOBALS['HTTP_RAW_POST_DATA'];
} else {
$input = "";
}
$xmlArray = Main::xml2array($input, $get_attributes = 0);
if (!empty($xmlArray['Es_Request']['Data']['UserName']) && !empty($xmlArray['Es_Request']['Data']['Password']))
{
$username = $xmlArray['Es_Request']['Data']['UserName'];
$password = sha1($xmlArray['Es_Request']['Data']['Password']);
}
/* Comment below code line when integratin with android device
*
*/
/*$username ="demodemo";
$password = sha1(123456);
$email='this@hotmail.com';*/
// Get the respective model instance
//print_r($username);
//exit;
switch($_GET['model'])
{
case 'list':
$models = Individual::model()->findAll();
case 'individual':
$models = Individual::model()->findAll('id=2');
break;
case 'login':
$models = Individual::model()->findByAttributes(array('username'=>$username,'password'=>$password));
break;//print_r($models->attributes);
case 'forgotpassword':
$models = Individual::model()->findByAttributes(array('email'=>$email));
break;
default:
// Model not implemented error
$this->_sendResponse(501, sprintf(
'Error: Mode <b>list</b> is not implemented for model <b>%s</b>',
$_GET['model']) );
Yii::app()->end();
}
// Did we get some results?
if($_GET['model']=='login'){
if(count($models)==0) {
// No
//echo "i am here";
$this->_sendResponse(200,
sprintf('No items where found for model <b>%s</b>', $_GET['model']) );
} else {
// Prepare response
/* print_r($_GET['model']);
exit; */
$this->_checkAuth($username,$password);
$xmldoc = '<?xml version="1.0" encoding="utf-8"?>';
$xmldoc.='<Es_Response>';
$xmldoc.='<Bpn_Id> </Bpn_Id>'; //The UID of the business partner.
$xmldoc.='<Es_Session_Id> </Es_Session_Id>';
$pcode = 1;
$scode = 'Successful';
$xmldoc.='<Response_Code>';
$xmldoc.='<Pcode><![CDATA['.$pcode.']]></Pcode>';
$xmldoc.='<Scode><![CDATA['.$scode.']]></Scode>';
$xmldoc.='</Response_Code>';
$xmldoc.='<Data> ';
$xmldoc.='<UserInfo>';
$xmldoc.='<UserId><![CDATA['.$models->id.']]></UserId>';
$xmldoc.='<UserAccessToken><![CDATA['.$models->token.']]></UserAccessToken>';
$xmldoc.='<Name><![CDATA['.$models->firstname.' '.$models->lastname.']]></Name>';
$xmldoc.='<Email><![CDATA['.$models->email.']]></Email>';
$xmldoc.='<Phone><![CDATA['.$models->phone.']]></Phone>';
$xmldoc.='<Address><![CDATA['.$models->address->addressline.','.$models->address->city.','.$models->address->zone.','.$models->address->country->name.']]></Address>';
$xmldoc.='<Description><![CDATA['.$models->description.']]></Description>';
$xmldoc.='</UserInfo>';
$xmldoc.='</Data> ';
$xmldoc.='</Es_Response>';
// Send the response
//$this->_checkAuth();
$this->_sendResponse(200, $xmldoc);
}
}
if($_GET['model']==forgotpassword){
if(empty($models)) {
// No
$this->_sendResponse(200,
sprintf('No items where found for model <b>%s</b>', $_GET['model']) );
}
else{
$xmldoc = '<?xml version="1.0" encoding="utf-8"?>';
$xmldoc.='<Es_Response>';
$xmldoc.='<Bpn_Id> </Bpn_Id>'; //The UID of the business partner.
$xmldoc.='<Es_Session_Id> </Es_Session_Id>';
$xmldoc.='<Data>';
$xmldoc.='<UserInfo>';
$xmldoc.='<UserId><![CDATA['.$models->id.']]></UserId>';
$xmldoc.='<UserAccessToken><![CDATA['.$models->token.']]></UserAccessToken>';
$xmldoc.='<Email><![CDATA['.$models->email.']]></Email>';
$xmldoc.='</UserInfo>';
$xmldoc.='</Data>';
$xmldoc.='</Es_Response>';
$this->_sendResponse(200, $xmldoc);
}
}
}
}
YII 开发者说,Input 变空了。我不明白。它实际上用非 MVC 框架 PHP 的话很好,但在 YII 的 MVC 框架中,它不起作用。
所以请给我一些技巧,如何使用 xml 从 android 发布数据到 YII 服务器。在上面我的代码中有什么实际错误。