0

I have an android app which makes a post function sending data to a Symfony project. Then I want to get these data and storage in my bd. I think the problem is in my php code and not in android's. I have tried in a lot of ways but with no result.

My android's post function:

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

The routing for a new user:

new_user:
    pattern: /new_user
    defaults: { _controller:TortillatorAPITortillatorBundle:Default:newUser}

And my Symfony function in my controller:

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;
}
3
  • What happens? any errors? Commented Apr 14, 2014 at 12:52
  • 1
    getRequest is deprecated though. Commented Apr 14, 2014 at 12:53
  • When I execute my android function it doesn't get back a response so it doesn't happen nothing Commented Apr 14, 2014 at 13:08

1 Answer 1

3

Just some Errors i saw, which are too big for a comment:

Change this part:

And my Symfony function in my controller:

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

into this, if your using annotations, otherwise you have to specify routing and method differently, aswell I've changed the Request Object.:

/**
*@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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.