Ferdinand Mwinje
Ferdinand Mwinje

Reputation: 1

Laravel Server doesn't receive http post request from android

I am trying to create an android web based app,the plan is to use php as the server side scripting language.First I used xampp,shared the network of my phone with the laptop using wifi-tethering,just as a try I created and returned a simple json object,it worked. Moving on to actually create a serious project I planned on using laravel to simplify the server side scripting for me,but for some reason the server engine in laravel doesn't seem to get the requests.Below is the plain php script that works:

<?php
      class message{
	  public $title="";
	  public $content="";
	   
	  
	   
	  }
	  
	   $message=new message;
	   $message->title='Hi there';
	   $message->content='I am hoping we can meet later tonight!';
			  echo json_encode($message);
			
			 
?>

This is the code in my controller which doesn't even execute

<?php 
class MyController extends BaseController{
  public function getHome(){

  $message=new message;
  $message->title="My title";
  $message->content="Everythiing is very cool!";
     return json_encode($ujumbe);
  }
}
?>

This is the code in my routes.php file in laravel

Route::post('/',array(
  'as' => 'home',
  'uses' => 'MyController@getHome'
));

Route::get('/',array(
  'as' => 'home',
  'uses' => 'MyController@getHome'
));

I expect the post route to be reached but that doesn't happen I have checked with logs but it seems as if there was never any request at all.But when I use my browser and type the same route i.e

http:://localhost/
I get the text representation of my object,so it works.

I have searched for similar problems I couldn't get anything helpful.By the way,on my android app I am not using localhost,I use the ip address from my wireless network.The code in my AsyncTask in is as below:

private class MyAsyncTask extends AsyncTask<String,String,String>{
    	 String url="http://192.168.43.215:8000/";
   		@Override
   		protected String doInBackground(String... arg0) {
   			DefaultHttpClient hclient=new DefaultHttpClient(new BasicHttpParams());
   			HttpPost hpost=new HttpPost(url);
   			hpost.setHeader("Content-type","application/json");
//  			hpost.setHeader("Content-Type",
//                    "application/x-www-form-urlencoded;charset=UTF-8");
   			InputStream inStream=null;
   			String result=null;
   			try{
   				HttpResponse hresponse=hclient.execute(hpost);
   				Log.v("ALERT","response is back");
   				HttpEntity entity=hresponse.getEntity();
   				inStream=entity.getContent();
   				BufferedReader reader=new BufferedReader(new InputStreamReader(inStream,"UTF-8"));
   				StringBuilder sbuilder=new StringBuilder();
   				String line=null;
   				while((line = reader.readLine()) != null){
   					sbuilder.append(line+"\n");
   				}
   				result=sbuilder.toString();
   				Log.v("ALERT","string built already");
   			}catch(Exception e){
   				e.printStackTrace();
   			}
   			finally{
   				try{
   					if(inStream!=null)
   						inStream.close();
   				}catch(Exception e){
   					e.printStackTrace();
   				}
   			}
   			Log.v("ALERT",result);
   			return result;
   			
   		}
    

   		@Override
   		protected void onPostExecute(String result) {
   			
   			super.onPostExecute(result);
   			JSONObject jobject;
   			Log.v("ALERT",result);
   			try{
   				jobject=new JSONObject(result);
   				txtTitle.setText(jobject.getString("title"));
   				txtContent.setText(jobject.getString("content"));
   			}catch(JSONException e){
   				
   				e.printStackTrace();
   			
   			}
   			
   		
   		}
My question is whether I am missing something regarding laravel routes when requests are not coming from the same host..and why when I try the same app with a server running the same script on xampp it works.Or if laravel doesn't support this kind of requests.Please help me with anything you can think of

Upvotes: 0

Views: 1094

Answers (1)

Omar Salim
Omar Salim

Reputation: 55

In String url="http://192.168.43.215:8000/";

Try String url="http://192.168.43.215

As you said, you tried http://localhost and it worked, so why send request to port 8000 ?

Upvotes: 1

Related Questions