如何在Android中通过改装从Laravel接收字符串响应?

时间:2018-11-27 11:22:16

标签: android laravel retrofit

我想将数据发布到laravel并从服务器接收字符串响应,但是我得到了

  

500内部服务器错误

我的代码有什么问题?

改造是:

 @POST("/saveInfo")
Call<String> sendInfo(
        @Query("name") String name,
        @Query("family") String family,
        @Query("age") String age

片段中:

 public   void saveInfo(String name,String  family,String age){
    Call<String> call = planApi.sendInfo(name,family,age);
    call.enqueue(new Callback<String>() {
        @Override
        public void onResponse(Call<String> call, retrofit2.Response<String> response) {
            if (response.isSuccessful()) {
                try {
                    if(response.body()=="success")
                        Toast.makeText(view.getContext(), "saved successfully ", Toast.LENGTH_SHORT).show();
                    else
                        Toast.makeText(view.getContext(), "saved failed!", Toast.LENGTH_SHORT).show();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }else{
                Log.e(TAG," Response Error:"+String.valueOf(response.code())+response.message());
                Toast.makeText(view.getContext(), response.message(), Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {
            Log.e(TAG," Response Error :"+t.getMessage());
            Toast.makeText(view.getContext(), "conection failed!", Toast.LENGTH_SHORT).show();
        }
    });
}

路由器是:

Route::post('/saveInfo','BoController@saveInfo');

和功能是:

public function saveInfo(Request $request)
{
    $student = new Student();
    $student->name = $request->name;
    $student->family = $request->family;
    $student->age= $request->age;
    if($student->save())
       echo "success";
    else
       echo "failed!";
}

问题解决了! 应该在kernel.php文件中注释“ \ App \ Http \ Middleware \ VerifyCsrfToken :: class”行,并在PHP函数类型“ echo(json_encode(“ success / failed”))“中也使用” @FormUrlEncoded“和@Field进行改型

3 个答案:

答案 0 :(得分:0)

您的问题有1个困惑。

@POST("/saveInfo")
Call<String> sendInfo(
    @Query("name") String name,
    @Query("family") String family,
    @Query("age") String age

是此POST API还是GET API,因为您使用的是与GET API相关的@Query批注,但您将此API声明为@POST。这可以帮助您解决问题。

答案 1 :(得分:0)

您不能将@Query与POST方法一起使用。您应该在每个参数之前和@POST上方使用@Field方法,并添加@FormUrlEncoded。

@FormUrlEncoded
@POST("/saveInfo")
Call<String> sendInfo(
    @Query("name") String name,
    @Query("family") String family,
    @Query("age") String age

并在此处的基本链接中添加“ /”。 .php的结尾也在哪里?

答案 2 :(得分:0)

错误出在您的laravel函数saveInfo()中。它没有返回json响应。以此格式

JSON响应

{
   "name":"value",
   "family":"value",
   "age":"value"
}

要解决此问题,请修改

public function saveInfo(Request $request)
{
    $student = new Student();
    $student->name = $request->name;
    $student->family = $request->family;
    $student->age= $request->age;
    if($student->save())
       echo "success";
    else
       echo "failed!";
}

对此

public function saveInfo(Request $request)
{
    $student = new Student();
    $student->name = $request->name;
    $student->family = $request->family;
    $student->age= $request->age;
    $message = "";
    if($student->save()) 
    {
        $message ="success";
    } 
    else 
    {
        $message = "failed!";
    }
    return response()->json(["message" => $message],200);
}

您可以在此处访问此链接以获取有关Laravel Json响应的更多信息。 Laravel API Errors and Exceptions: How to Return Responses

Laravel HTTP响应文档的HTTP Responses

Android使用json Retrofit Android Example with Get and Post Api Request.改造POST AND GET