我无法使用Android应用程序使用我的Laravel 4 API。 但我无法做到
这是我的routes.php
Route::group(['prefix' => 'api'], function(){
Route::group(['prefix' => 'v1', 'namespace' => 'api\v1'], function(){
Route::resource('person', 'PersonsRestController', array('only' => array('store', 'index')));
});
});
这是我的PersonsRestController.php
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']);
}
}
我试图进行STORE操作(POST http方法),但我总是得到INDEX的消息
我使用此代码测试我的API休息:
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);
}
}
代码来源:https://stackoverflow.com/a/21657510/4359029
我希望有人能帮我解决问题...
修改
我也直接尝试过这个:
Route::post('test', 'MyTestController@testMethod');
但是我收到了下一个错误:Server returned HTTP response code: 405 for URL: http://foo.bar/api/laravel
所以laravel没有找到我的邮寄路线