how to post data from android to laravel 3 controller

744 views Asked by At

Hello guys i am using laravel 3, i want to make post request to laravel 3 controller. here is coontroller users.php

      class Users_Controller extends Base_Controller
       {
           public $restful = true;

          public function post_signin()
          {
                 print("request success");
          }
        }

Here is routes.php

         Route::get('signin', array('as'=>'signin','uses'=>'users@signin'));
         Route::post('signin', array('uses'=>'users@signin'));

here is android post request

          httppost= new HttpPost("http://test.com/signin");

test.com is local domain i am using through host file. what url should i enter in android to make post request? so that it can go to users.php and execute post_signin method of that file.

i tried following

   1) httppost= new HttpPost("http://test.com/signin");  (as per my route of routes.php)

2)httppost= new               HttpPost("http://localhost/androidtest/application/controller/users.php");

   3) httppost= new HttpPost("http://test.com/users/signin");
   (as per this referance (http://stackoverflow.com/questions/16137851/url-to-do-request-on-laravel)
1

There are 1 answers

2
user2432612 On

Based on your comment you are executing request two times but with different methods (execute). You just need one execute in order to make request to server. You are also using response variable as string and as HttpResponse object.

Please try to use code below to make your request, change url with your url

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);

HttpResponse hResponse = httpClient.execute(httpPost);
StatusLine sl = hResponse.getStatusLine();
int statusCode = sl.getStatusCode();

if ( statusCode == 200 ){
    HttpEntity hp = hResponse.getEntity();
    InputStream content = hp.getContent();
    BufferedReader reader = new BufferedReader(new InputStreamReader(content));

    StringBuilder builder = new StringBuilder();

    String line;
    while((line = reader.readLine()) != null){
        builder.append(line);
    }

    String responseBody = builder.toString();

    System.out.println("Response : " + responseBody); 
}