How to post data from android to yii framework server getting error

陌路散爱 提交于 2019-12-11 02:20:06

问题


I am suffering problem during getting value from yii framework sever. From yii developer side , he says me you are nothing giving me any input data, but i all ready give data from xml format. He says, i am getting imput null.

YII developer Said to me: He getting null in input

$input = $GLOBALS['HTTP_RAW_POST_DATA'];

Why input is null, would you guys any idea behind this problem.

My android side:

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);

and HttpPostRetreiver.java file

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;

    }

}

Here I am getting response like this:

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>

and From the YII Developer Side:

He did like this:

<?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 Developer said, Input getting empty. I did not understand. it actually words fine non MVC Framework PHP, but in MVC framework in YII, it not working.

So please give me so tricks, How to post data using xml from android to YII server . and in Above what actually wrong in my code.


回答1:


I would not access directly superglobals in yii

there are

Yii::app()->request->getQuery() 

or

Yii::app()->request->getPost()

If you really need to get the HTTP_RAW_POST_DATA

<?php $postdata = file_get_contents("php://input"); ?> 

http://php.net/manual/en/reserved.variables.httprawpostdata.php



来源:https://stackoverflow.com/questions/18169107/how-to-post-data-from-android-to-yii-framework-server-getting-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!