我尝试使用laravel api进行简单的身份验证。但不幸的是,在我的android工作室项目中,它返回了响应代码422。
以下是我的API中的代码:
LoginController.php
public function login(Request $request)
{
$this->validateLogin($request);
if ($this->attemptLogin($request)) {
$user = $this->guard()->user();
$user->generateToken();
return response()->json([
'data' => $user->toArray(),
]);
}
return $this->sendFailedLoginResponse($request);
}
然后在我的Android Studio界面中:
BizWebService.java
@Headers({
"Content-Type: application/json",
"Accept: application/json"
})
@POST("api/login")
@FormUrlEncoded
Call<LoginResponse> login(
@Field("username") String username,
@Field("password") String password);
class LoginResponse extends ServiceResponse {
@SerializedName("message")
public String message;
@SerializedName("errors")
public ArrayList<Email> errors;
@SerializedName("data")
public ArrayList<DataItems> data;
}
class DataItems {
@SerializedName("id")
@Expose
public int id;
@SerializedName("username")
@Expose
public String username;
@SerializedName("api_token")
@Expose
public String api_token;
@SerializedName("whoToVote")
@Expose
public String whoToVote;
@SerializedName("created_at")
@Expose
public String created_at;
@SerializedName("updated_at")
@Expose
public String updated_at;
}
class Email {
@SerializedName("email")
@Expose
public String email;
}
模块类:
public class Module {
public static void register(BizApplication application) {
BizWebService api = createWebService(application);
new LiveAccountService(api, application);
new LiveCandidateService(api, application);
new LiveCriteriaService(api, application);
}
private static BizWebService createWebService(BizApplication application) {
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new AuthInterceptor(application.getAuth()))
.connectTimeout(100, TimeUnit.SECONDS)
.readTimeout(100, TimeUnit.SECONDS)
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BizApplication.API_ENDPOINT.toString())
.client(client)
.addConverterFactory(GsonConverterFactory.create(new Gson()))
.build();
BizWebService webService = retrofit.create(BizWebService.class);
return webService;
}
private static class AuthInterceptor implements Interceptor {
private final Auth auth;
private AuthInterceptor(Auth auth) {
this.auth = auth;
}
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (auth.hasAuthToken()) {
request = request
.newBuilder()
.addHeader("Authorization", "Bearer " + auth.getAuthToken())
.build();
}
long t1 = System.nanoTime();
Log.i("Request", String.format("Sending request %s on %s%n%s",
request.url(), chain.connection(), request.headers()));
Response response = chain.proceed(request);
if (response.isSuccessful()) {
return response;
}
if (response.code() == 401 && auth.hasAuthToken()) {
auth.setAuthToken(null);
}
long t2 = System.nanoTime();
Log.i("Response", String.format("Received response for %s in %.1fms%n%s",
response.request().url(), (t2 - t1) / 1e6d, response.headers()));
return response;
}
}
}
LiveAccountService.java
public class LiveAccountService extends BaseLiveService {
private final Auth auth;
public LiveAccountService(BizWebService api, BizApplication application) {
super(api, application);
auth = application.getAuth();
}
@Subscribe
public void loginUserRequest(Account.LoginRequestRequest request) {
Call<BizWebService.LoginResponse> call = api.login(request.Username, request.Password);
call.enqueue(new RetrofitCallback<BizWebService.LoginResponse>(BizWebService.LoginResponse.class) {
@Override
protected void onResponseSuccess(Call<BizWebService.LoginResponse> t, Response<BizWebService.LoginResponse> response) {
if (response.isSuccessful()) {
return;
} else {
Account.LoginRequestResponse response1 = new Account.LoginRequestResponse();
response1.setOperationError("Error");
bus.post(response1);
Log.e("Server Error", Integer.toString(response.code()));
}
}
});
}
我尝试在Postman中发布请求,但它确实有效。 我不知道我做错了什么。任何帮助将不胜感激。
答案 0 :(得分:0)
我知道这是我的老职位,但我只想分享我如何解决该问题。这不是真正的解决方案,但是它可以帮助我解决问题。
基本上,我的核心问题是我看不到我在Web服务上抛出的请求和响应,因此我安装了第3方库,该库监视我抛出的请求和从Web服务接收到的响应。服务器。它是HttpLoggingInterceptor。然后,在createWebService
方法中,将其添加为我的OkHttpClient对象中的拦截器之一。那时,我能够监视请求和响应,并且能够将正确且可接受的数据发送到服务器。