当我尝试从android发布我的JSON时,Zend $ _POST ['param_name']说“Undefined index”

my android function : im sure that there is no problem with MAP i put in the function

    public class WebServiceController extends  AsyncTask<WebServiceProperties, Void,   String> {

public static String makeRequest(String path, Map<?, ?> params ) throws Exception {

     HttpParams httpParameters = new BasicHttpParams();
        int timeoutConnection = 10000;
        HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
        DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters);

        // url with the post data
        HttpPost httpost = new HttpPost(path);

        // convert parameters into JSON object
        JSONObject holder = new JSONObject(params);

        // passes the results to a string builder/entity
        StringEntity se = new StringEntity(holder.toString());

        // sets the post request as the resulting string
        httpost.setEntity(se);

        // sets a request header so the page receiving the request
        // will know what to do with it
        httpost.setHeader("Accept", "application/json");
        httpost.setHeader("Content-type", "application/json");


        // Handles what is returned from the page
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String httpResponse = httpclient.execute(httpost, responseHandler);
        return httpResponse;
  }


 @Override
 protected String doInBackground(WebServiceProperties... params) {
// TODO Auto-generated method stub
int count = params.length;
for (int i = 0; i < count; i++) {
    WebServiceProperties sp=params[i];
    try {
        return makeRequest(sp.getPath(), sp.getParams());
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
return null;
 }



   }

this is my PHP controller :

 <?php

class ServiceController extends Zend_Controller_Action
{

public function init()
{

     $this->_response->setHeader('Content-Type', 'appication/json');
    $this->_response->setHeader('Access-Control-Allow-Origin', '*');
    $this->_response->setHeader('Access-Control-Max-Age', '3628800');
    $this->_response->setHeader('Access-Control-Allow-Methods', ' GET, POST, PUT,   DELETE');
    $this->_helper->viewRenderer->setNoRender(true);
    $this->_helper->layout->disableLayout(); 
  }

   public function indexAction()
  {



  }


  public function getVenuesAction()
  {
    $M_Venue = new Model_Venues();
    $venues = $M_Venue->selectVenuesDetails();
    echo Zend_Json::encode($venues);
  }
  public function getAuthAction()
  {
    $msg="";
    $res=false;
    $method=$_SERVER['REQUEST_METHOD'];
    try{
        $password=$_POST['password'];
        $venue_id=$_POST['venue_id'];
        $realpassword=md5($password);
        $mvenue=new Model_Venues();
        $venueRes=$mvenue->venueAuth($venue_id, $realpassword);
        $res=($venueRes)?true:false;
    }  catch (Exception $exc)
    {
        $msg=$exc->getMessage();
    }
    echo Zend_Json::encode(array("success"=>$res,'msg'=>$msg,'method'=>$method));
  }

}

$_POST['password'] and $_POST['venue_id'] throw "undefined index";

this is a photo captured when i debug params enter image description here

doushui3216
doushui3216 谢谢,你们都帮了我一天
大约 6 年之前 回复
dongshao1981
dongshao1981 请注意$json_data必须是控制器类的受保护成员,然后您将在getAuthAction方法而不是$_POST变量中引用它
大约 6 年之前 回复
dongyang2229
dongyang2229 你可以在控制器的初始化中执行类似这样的操作$request=$this->getRequest();$rawBody=$request->getRawBody();$json_data=Zend_Json::decode($rawBody);
大约 6 年之前 回复
dongyu3967
dongyu3967 请教我如何愚蠢如此抱歉。我的意思是我可以在哪里解码它
大约 6 年之前 回复
douan3182
douan3182 如果发布json字符串,则不会设置POST参数。你必须先json_decode。
大约 6 年之前 回复

1个回答



只是为了充实我的评论。 这一行应该做的事情:</ p>

  class ServiceController扩展Zend_Controller_Action 
{
protected $ json_data = array();

公共函数init()
{

$ this-&gt; _response-&gt; setHeader('Content-Type','appication / json');
$ this-&gt; _response-&gt ; setHeader('Access-Control-Allow-Origin','*');
$ this-&gt; _response-&gt; setHeader('Access-Control-Max-Age','3628800');
$ this - &gt; _response-&gt; setHeader('Access-Control-Allow-Methods','GET,POST,PUT,DELETE');
$ this-&gt; _helper-&gt; viewRenderer-&gt; setNoRender(true);

$ this-&gt; _helper-&gt; layout-&gt; disableLayout();

$ request = $ this-&gt; getRequest();

$ rawBody = $ request-&gt; getRawBody();

$ json_data = Zend_Json :: decode($ rawBody);
}

公共函数getAuthAction(){
//代码....
$ password = $ json_data ['password']; \ n $ venue_id = $ json_data ['venue_id'];
//代码.....
}
}
</ code> </ pre>
</ div>

展开原文

原文

Just to flesh out my comments. Something along this line should do it:

class ServiceController extends Zend_Controller_Action
{
  protected $json_data = array(); 

  public function init()
  {

    $this->_response->setHeader('Content-Type', 'appication/json');
    $this->_response->setHeader('Access-Control-Allow-Origin', '*');
    $this->_response->setHeader('Access-Control-Max-Age', '3628800');
    $this->_response->setHeader('Access-Control-Allow-Methods', ' GET, POST, PUT,   DELETE');
    $this->_helper->viewRenderer->setNoRender(true);
    $this->_helper->layout->disableLayout(); 
    $request = $this->getRequest(); 
    $rawBody = $request->getRawBody(); 
    $json_data = Zend_Json::decode($rawBody);
  }

  public function getAuthAction() {
  // code....
        $password=$json_data['password'];
        $venue_id=$json_data['venue_id'];
  // code.....
  }
}

dongmaopiao0901
dongmaopiao0901 +1谢谢先生,你很清楚
大约 6 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问