Android Retrofit + Laravel 5.3带护照:401未经授权

时间:2017-07-01 09:32:45

标签: android laravel-5.3 retrofit http-status-code-401 laravel-passport

我的Android应用程序正在发布并从Laravel 5.3服务器获得响应。我正在使用Laravel Passport进行api身份验证。我能够从我的应用程序成功登录并生成访问令牌,所有其他Api也会通过身份验证,除了一个。在注册帐户后的应用程序医生必须提交患者详细信息,这是在服务器上给出401错误的那个,但它在本地服务器上工作。 这是我的android界面,

@Multipart
@POST("/api/submitnewpatient")
Call<ServerResponse> submitPatient(
        @Part("image") RequestBody text,
        @Part List<MultipartBody.Part> images,

        @Query("to_favourite") String to_favourite,
        @Query("refer_to")  String refer_to,
        @Query("patient_first_name") String patient_first_name,
        @Query("patient_last_name") String  patient_last_name,
        @Query("patient_gender") String patient_gender,
        @Query("patient_age") String    patient_age,
        @Query("patient_contact_number") String patient_contact_number,
        @Query("patient_blood_group") String    patient_blood_group,
        @Query("patient_allergies") String  patient_allergies

);

这是改造代码

HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder()
            .connectTimeout(100, TimeUnit.SECONDS)
            .readTimeout(80, TimeUnit.SECONDS)
            .addInterceptor(
                    new Interceptor() {
                        @Override
                        public okhttp3.Response intercept(Interceptor.Chain chain) throws IOException {
                            Request original = chain.request();
                            Request.Builder requestBuilder = original.newBuilder()
                                    .header("Authorization", "Bearer " + token)
                                    .header("Accept", "application/json")
                                    .header("Application", "application/json");
                            Request request = requestBuilder.build();
                            return chain.proceed(request);
                        }
                    }
            );
    //OkHttpClient client = httpClient.build();

    httpClient.addInterceptor(logging);
    Retrofit retrofit = new Retrofit.Builder()
            .client(httpClient.build())
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl(SERVER_URL)
            .build();
    Interface service = retrofit.create(Interface.class);

Call<ServerResponse> call = service.submitPatient(filename, fileToUpload, spinner_fav.getSelectedItem().toString(), String.valueOf(spinner_refer.getSelectedItemPosition()+1),patientFNameET.getText().toString().trim(),patientLNameET.getText().toString().trim(),spinner_gender.getSelectedItem().toString(),patient_ageET.getText().toString().trim(),patientContectnumberET.getText().toString().trim(),patientBloodGroupET.getText().toString().trim(),patientAllergiesET.getText().toString().trim(),minBedChargeEt.getText().toString().trim(),maxBedChargeEt.getText().toString().trim(),addmitedForET.getText().toString().trim(),dateofAdmissionET.getText().toString().trim(),likelyDaysAdmissionET.getText().toString().trim(),operationNeed);

        call.enqueue(new Callback<ServerResponse>() {
            @Override
            public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
                progressDialog.dismiss();
                SubmitNewPatient.this.finish();
                Intent intent = new Intent(SubmitNewPatient.this, DoctorActivity.class);
                //intent.putExtra("statusId", String.valueOf(statusid[getAdapterPosition()]));
                startActivity(intent);

            }

            @Override
            public void onFailure(Call<ServerResponse> call, Throwable t) {
                // handle execution failures like no internet connectivity
                BusProvider.getInstance().post(new ErrorEvent(-2, t.getMessage()));
                progressDialog.dismiss();
                Toast.makeText(SubmitNewPatient.this, "Something is Wrong", Toast.LENGTH_LONG).show();
            }
        });

这是Laravel路线

Route::post('submitnewpatient', [
     'uses' => 'PatientApiController@submitPatient',
     'as'   => 'post.submitnewpatient',
     'middleware'   => 'api'

])->middleware('auth:api');

Laravel控制器

public function submitPatient(Request $request){
    $allImages = $request->files->get('images');
    $patientSubimitted = $request->user()->submittedPatient()->create([

        'to_favourite'  => $request->input('to_favourite'),
        'refer_to'  => $request->input('refer_to'),
        'patient_first_name'    => $request->input('patient_first_name'),
        'patient_last_name' => $request->input('patient_last_name'),
        'patient_gender'    => $request->input('patient_gender'),
        'patient_age'   => $request->input('patient_age'),
        'patient_contact_number'    => $request->input('patient_contact_number'),
        'patient_blood_group'   => $request->input('patient_blood_group'),
        'patient_allergies' => $request->input('patient_allergies'),

    ]);

    $destinationPath = 'reports';
    $path = base_path() . '\public\reports';

    $count = 0;

    if(Input::file('images')){

        foreach ($request->file('images') as $img ) {
            $imageName = time().substr( base_convert( time(), 10, 36 ).md5( microtime() ), 0, 16 ).$img->getClientOriginalName();

            $upload_success = $img->move($destinationPath, $imageName);

            $patientSubimitted->patientReports()->create([
                'report_by' => $request->user()->id,
                'report_path' => $imageName,

            ])->user()->associate($request->user());
        }
        //return $allImages;

    }

    return response()->json(['success' => 'Patient Submitted']);
} 

0 个答案:

没有答案
相关问题