2015-12-17 53 views
0

我在Symfony控制器中检索POST数据时遇到问题。来自Android的POST请求未在Symfony控制器中收到

每当我打印帖子,获取或文件变量,数组是空的。

编辑:我使用的是10.0.2.2

在这里,在本地环境中测试这就是我发送数据:

HttpClient httpClient = new DefaultHttpClient(); 
HttpPost httpPost = new HttpPost(mContext.getString(R.string.url_home) + "en" + mContext.getString(R.string.save_sentences_route) + "/add"); 
HttpResponse response = null; 
try { 
    List<NameValuePair> params = new ArrayList<NameValuePair>(1); 
    params.add(new BasicNameValuePair("text", "A")); 
    httpPost.setEntity(new UrlEncodedFormEntity(params)); 

    response = httpClient.execute(httpPost); 
    HttpEntity entity = response.getEntity(); 
    Log.i("TEST", "Passed httpClient.execute"); 
} 
catch (Exception ignored) 
{ 
} 

这里就是我如何接受它在Symfony的控制器:

$user = $this->get('security.context')->getToken()->getUser(); 
if (!is_object($user) OR !$this->get('security.context')->isGranted('IS_AUTHENTICATED_REMEMBERED')) 
{ 
return new RedirectResponse($this->generateUrl('test_core_default_index')); 
} 

$all_users = $this->getDoctrine()->getManager()->getRepository('MyUserBundle:User'); 
$all_users = $all_users->findAll(); 

var_dump($_GET); 
echo "</br></br></br><hr></br></br></br>"; 
var_dump($_POST); 
echo "</br></br></br><hr></br></br></br>"; 
var_dump($_FILES); 
echo "</br></br></br><hr></br></br></br>"; 

//deliberate bug in order to see the var_dumps and echos 
return new RedirectResponse($this->generateUrl('deliberate_bug')); 

我第一次与这些测试,而不是$ _POST和$ _GET:

$this->getRequest()->request->all(); 
$this->getRequest()->query->all(); 

,并与这些还有:

$request->get('text'); 
$request->get('A'); //I was really desperate trying this. 

还测试$ _FILES, “以防万一”。

我真的不是我做错了什么。任何想法 ?

在此先感谢。

回答

0

你有没有检查cors? 你可以使用这个包很容易地做到这一点 NelmioCorsBundle

+1

我没有,因为我想得到它与更少的束可能工作。 虽然我会研究它,因为除了你以外没有人回答。谢谢:) –

+0

你可以在htaccess中定义它,但对于我来说这个包很有帮助 – ThomasP1988

相关问题