我正在尝试创建一个基于android网络的应用程序,计划使用php作为服务器端脚本语言。首先,我使用了xampp,使用wifi共享我的手机网络和笔记本电脑,就像我创建并返回一个简单的json对象一样,它起了作用。接下来,我计划创建一个严肃的项目,使用laravel来简化服务器端脚本,但由于某种原因,laravel中的服务器引擎似乎无法接收请求。下面是一个简单的php脚本:
<?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);
?>
这是我的控制器中的代码,它甚至不执行
<?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);
}
}
?>
这是我在laravel中的routes.php文件中的代码
Route::post('/',array(
'as' => 'home',
'uses' => 'MyController@getHome'
));
Route::get('/',array(
'as' => 'home',
'uses' => 'MyController@getHome'
));
我预计会到达邮寄路线,但这并没有发生。我已经查看了日志,但似乎根本没有任何请求。但当我使用浏览器并键入相同的路线时,即
http:://localhost/
我得到了对象的文本表示,所以它可以工作。我搜索过类似的问题,但没有得到任何帮助。顺便说一句,在我的android应用程序上,我没有使用localhost,而是使用无线网络的ip地址。中的AsyncTask中的代码如下所示:
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();
}
}
我的问题是,当请求不是来自同一主机时,我是否遗漏了关于laravel路由的内容。。以及为什么当我在xampp上运行相同脚本的服务器上尝试相同的应用程序时,它会起作用。或者如果拉拉威尔不支持这种请求。请帮我做任何你能想到的在String url="http://192.168.43.215:8000/";
中
尝试String url="http://192.168.43.215
正如您所说,您尝试了http://localhost
,并且它有效,那么为什么要将请求发送到8000端口呢?