Java LaravelAPI混淆了POST方法和GET方法
我无法用android应用程序使用我的Laravel4API但我不能那样做 这是我的Java LaravelAPI混淆了POST方法和GET方法,java,php,android,rest,laravel-4,Java,Php,Android,Rest,Laravel 4,我无法用android应用程序使用我的Laravel4API但我不能那样做 这是我的routes.php Route::group(['prefix' => 'api'], function(){ Route::group(['prefix' => 'v1', 'namespace' => 'api\v1'], function(){ Route::resource('person', 'PersonsRestController', array('on
routes.php
Route::group(['prefix' => 'api'], function(){
Route::group(['prefix' => 'v1', 'namespace' => 'api\v1'], function(){
Route::resource('person', 'PersonsRestController', array('only' => array('store', 'index')));
});
});
namespace api\v1;
class PersonsRestController extends \BaseController {
// The method is GET
public function index() {
dd("Index: ".$_SERVER['REQUEST_METHOD']);
}
// The method is POST
public function store() {
dd("Store: ".$_SERVER['REQUEST_METHOD']);
}
}
这是我的PersonsRestController.php
Route::group(['prefix' => 'api'], function(){
Route::group(['prefix' => 'v1', 'namespace' => 'api\v1'], function(){
Route::resource('person', 'PersonsRestController', array('only' => array('store', 'index')));
});
});
namespace api\v1;
class PersonsRestController extends \BaseController {
// The method is GET
public function index() {
dd("Index: ".$_SERVER['REQUEST_METHOD']);
}
// The method is POST
public function store() {
dd("Store: ".$_SERVER['REQUEST_METHOD']);
}
}
我试图做一个存储操作(POST http方法),但我总是得到INDEX的消息
我使用以下代码测试API rest:
public static void main(String[] args) throws Exception {
URL url = new URL("http://foo.com/api/v1/person/");
Map<String, Object> params = new LinkedHashMap<>();
params.put("name", "Freddie the Fish");
params.put("email", "fishie@seamail.example.com");
params.put("reply_to_thread", 10394);
params.put("message", "Shark attacks in Botany Bay have gotten out of control. We need more defensive dolphins to protect the schools here, but Mayor Porpoise is too busy stuffing his snout with lobsters. He's so shellfish.");
StringBuilder postData = new StringBuilder();
for (Map.Entry<String, Object> param : params.entrySet()) {
if (postData.length() != 0) {
postData.append('&');
}
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData.append('=');
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
conn.setDoOutput(true);
conn.getOutputStream().write(postDataBytes);
Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
for (int c = in.read(); c != -1; c = in.read()) {
System.out.print((char) c);
}
}
但我得到了下一个错误:服务器为URL返回了HTTP响应代码405:http://foo.bar/api/laravel
因此laravel没有找到我的发布路线正如我在文档中看到的,请使用laravel 4.2中的发布方法。我请求存储在我的代码中…您是否更改了错误消息,或者它是否真的返回:
http://foo.bar/api/laravel
?:)您没有为api/laravel
定义路由。