我有一个Android应用程序,它使一个post函数将数据发送到Symfony项目。然后我想在我的bd中获取这些数据和存储。我认为问题出在我的PHP代码中而不是在android的代码中。我已经尝试了很多方法,但没有结果。
我的机器人的帖子功能:
public String sendPost(String username, String password, String email, String city) {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost("https://192.168.1.120/TortillatorAPI/web/new_user");
HttpResponse response = null;
try
{
List<NameValuePair> params = new ArrayList<NameValuePair>(4);
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("city", city));
httpPost.setEntity(new UrlEncodedFormEntity(params));
response = httpClient.execute(httpPost, localContext);
}
catch (Exception e)
{
}
return response.toString();
}
新用户的路由:
new_user:
pattern: /new_user
defaults: { _controller:TortillatorAPITortillatorBundle:Default:newUser}
我的Symfony功能在我的控制器中:
public function newUserAction()
{
$request = $this->getRequest();
$user = new User();
$user->setUsername($request->get('username'));
$user->setPassword($request->get('password'));
$user->setEmail($request->get('email'));
$user->setCity($request->get('city'));
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
$response = new Response();
$response->setStatusCode(201);
return $response;
}
答案 0 :(得分:3)
我看到的一些错误,对于评论来说太大了:
更改此部分:
我的Symfony功能在我的控制器中:
public function newUserAction()
{
$request = $this->getRequest();
$user = new User();
$user->setUsername($request->get('username'));
$user->setPassword($request->get('password'));
$user->setEmail($request->get('email'));
$user->setCity($request->get('city'));
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
$response = new Response();
$response->setStatusCode(201);
return $response;
}
如果您使用注释,否则您必须以不同方式指定路由和方法,以及我已更改了请求对象。
/**
*@Route("YOURROUTE", name="YOURROUTE")
*@Method("POST")
*@Template()
*/
public function newUserAction(Request $request)
{
$user = new User();
$user->setUsername($request->get('username'));
$user->setPassword($request->get('password'));
$user->setEmail($request->get('email'));
$user->setCity($request->get('city'));
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
$response = new Response();
$response->setStatusCode(201);
return $response;
}