如何使用Laravel Api注册Android应用程序用户

时间:2019-05-06 17:03:14

标签: android laravel api retrofit2

我想使用laravel api注册用户。 Api既可以在服务器上运行,也可以在邮递员上工作,但在我的应用程序中却无法正常工作。我正在使用retrofit2库来使用它。这是错误

2019-05-06 17:23:44.134 473-473 / com.engrsoft.www.onlineservices E / response字符串:Response {protocol = http / 1.1,code = 200,message = OK,url = {{3 }}}

  

Api.java

public interface Api {

    @FormUrlEncoded
    @POST("register")

    Call<ResponseBody> register(
            @Field("name") String name,
            @Field("email") String email,
            @Field("password") String password
    );
  

RetrofitClient.java

public class RetrofitClient {

    private static final String BASE_URL="http://zattest.com.zawataafnantechnologies.com/newapi/api/";

    private static RetrofitClient retrofitClient;
    private Retrofit retrofit;

    private RetrofitClient(){
        retrofit= new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    public static synchronized RetrofitClient getRetrofitClient(){
        if (retrofitClient == null){
            retrofitClient = new RetrofitClient();
        }
        return retrofitClient;
    }
    public Api getApi(){
        return retrofit.create(Api.class);
    }
  

RegisterActivity.java

注册方法。反应来了,但没有成功

public void register(){

                        Call<ResponseBody> call = RetrofitClient
                                .getRetrofitClient()
                                .getApi()
                                .register(txt_username,txt_email,txt_password);
                        call.enqueue(new Callback<ResponseBody>() {
                                         @Override
                                         public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
                                             String s= response.toString();
                                             Log.e("response String", s);
                                             Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();

                                         }

                                         @Override
                                         public void onFailure(Call<ResponseBody> call, Throwable t) {
                                             Toast.makeText(getApplicationContext(),t.getMessage(),Toast.LENGTH_LONG).show();

                                         }
                                     }
                    }

1 个答案:

答案 0 :(得分:0)

此消息

2019-05-06 17:23:44.134 473-473/com.engrsoft.www.onlineservices E/response String: Response{protocol=http/1.1, code=200, message=OK

并不表示您所说的错误响应。实际上,这是一条OK /成功消息,表示请求已成功。

现在您不记录响应正文。因此,更改

String s= response.toString();

String s= response.body().toString();用于接收来自后端的正确消息