【问题标题】:Android App POST-ing to PHP Laravel ServerAndroid App POST-ing 到 PHP Laravel 服务器
【发布时间】:2023-03-20 23:35:01
【问题描述】:

我有一个 android 应用程序,我已设置将一些数据发布到我的 PHP Laravel 服务器,但是我收到 405 错误,我不知道为什么(尝试通过 Postman 发布工作正常)

这是我的安卓代码:

 String URL_POST = "http://192.168.2.102/device/id";
 btnGoi.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {

            InsertSV();
        }

    });


private void InsertSV() {

    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_POST, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {

            Toast.makeText(getApplication(), response, Toast.LENGTH_SHORT).show();

        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(IdDeviceActivity.this, error + "", Toast.LENGTH_SHORT).show();
        }
    }
    ) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {

            Map<String, String> params = new HashMap<String, String>();
            String Id = edtId.getText().toString();
            params.put("ID", Id);

            return params;
        }
    };

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

这是我的 PHP Laravel 之一(我正在使用 xampp 启动它):

public function checkDeviceId(Request $request){

    $id = $request->input('ID');
    $code=LicenceCodes::where('device_id',$id)->first();
    if($code==null){
        return response('Wrong device id.'.$id, 405)
            ->header('Content-Type', 'text/plain');
    }
    else
    {
        return response('Device added'.$id, 200)
            ->header('Content-Type', 'text/plain');
    }

还有我的路由:

Route::post('/device/id', 'DeviceController@checkDeviceId');

知道它与 Postman 一起工作得很好,我认为可能存在 android 问题?或者我没有正确传递参数,我真的不确定......

编辑:但是,我尝试 POST 到我的 xampp 文件夹中的一个简单的 php 文件,因此它工作正常

稍后编辑:我看到我在 Postman 上也得到了 405 Method Not allowed ,但是我仍然在正文中得到了良好的响应,这就是我被愚弄的原因。我应该如何修复 405 错误?

【问题讨论】:

    标签: php android laravel postman


    【解决方案1】:

    找到了。必须切换一些状态错误代码,现在可以正常工作了。

    【讨论】:

    • 您的回答当然可以更明确一点。它也可能对其他人有所帮助。
    • 我想我也有同样的问题。你能解释一下你是怎么解决的吗??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    相关资源
    最近更新 更多