Android Retrofit不适用于Laravel API端点

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (31)

我一直在试图弄清楚为什么最终的android改造没有发送POST。这就是我所做的。在Laravel我已经设置了模型,控制器,改为VerifyCsrfToken允许api。然后我设置了web和api的路由。所有都通过邮差测试,它的工作原理,但当我尝试通过Android发布代码失败。

Web.php

Route::get('/', function () {
return view('welcome');
});

Route::resource('/api/student', 'StudentsApiController');

Api.php

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

Route::post('student/store', 'StudentsApiController@store');

Android - api.java

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;

public interface Api {

    @FormUrlEncoded
    @POST("store")
    public Call<ResponseBody> store(
        @Field( "email" ) String email,
        @Field ( "password" ) String password,
        @Field ( "name" ) String name,
        @Field ( "school" ) String school
    );

}

Android - RetrofitClient.java

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public
class RetrofitClient {

    private static final String BASE_URL = "192.168.2.23/api/student/";
    private static RetrofitClient mInstance;
    private Retrofit retrofit;

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

    public static synchronized RetrofitClient getmInstance(){
        if(mInstance == null){
            mInstance = new RetrofitClient ();
        }
        return mInstance;
    }

    public Api getApi(){
        return retrofit.create ( Api.class );

    }
}

Android - MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public
class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText editTextEmail, editTextPassword, editTextName, editTextSchool;

    @Override
    protected
    void onCreate ( Bundle savedInstanceState ) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_main );

        editTextEmail = findViewById ( R.id.editTextEmail );
        editTextPassword = findViewById ( R.id.editTextPassword );
        editTextName = findViewById ( R.id.editTextName );
        editTextSchool = findViewById ( R.id.editTextSchool );

        findViewById ( R.id.buttonSignUp ).setOnClickListener ( this );

        findViewById ( R.id.textViewLogin ).setOnClickListener ( this );

    }

    private
    void userSignUp () {
        String email = editTextEmail.getText ( ).toString ( ).trim ( );
        String password = editTextPassword.getText ( ).toString ( ).trim ( );
        String name = editTextName.getText ( ).toString ( ).trim ( );
        String school = editTextSchool.getText ( ).toString ( ).trim ( );

        if (email.isEmpty ( )) {
            editTextEmail.setError ( "Email required" );
            editTextEmail.requestFocus ( );
            return;
        }

        if (Patterns.EMAIL_ADDRESS.matcher ( email ).matches ( )) {
            editTextEmail.setError ( "Email a valid email" );
            editTextEmail.requestFocus ( );
            return;
        }

        if (password.isEmpty ( )) {
            editTextPassword.setError ( "Password is required" );
            editTextPassword.requestFocus ( );
            return;
        }
        if (password.length ( ) < 6) {
            editTextPassword.setError ( "Password should be 6 characters long" );
            editTextPassword.requestFocus ( );
            return;
        }
        if (name.isEmpty ( )) {
            editTextName.setError ( "Name is required" );
            editTextName.requestFocus ( );
            return;
        }
        if (school.isEmpty ( )) {
            editTextSchool.setError ( "Name is required" );
            editTextSchool.requestFocus ( );
            return;
        }


    }

    @Override
    public
    void onClick ( View v ) {
        switch (v.getId ( )) {
            case R.id.buttonSignUp:
                userSignUp ( );
                break;
            case R.id.textViewLogin:
                break;
        }

        Call <ResponseBody> call = RetrofitClient
                .getmInstance ()
                .getApi ()
                .store("email", "password", "name", "school");

        call.enqueue ( new Callback <ResponseBody> ( ) {
            @Override
            public
            void onResponse ( Call <ResponseBody> call, Response<ResponseBody> response ) {
                try {
                    String s = response.body ().string ();
                    Toast.makeText ( MainActivity.this, s, Toast.LENGTH_LONG ).show ();
                } catch (IOException e) {
                    e.printStackTrace ( );
                }



            }

            @Override
            public
            void onFailure ( Call <ResponseBody> call, Throwable t ) {

                Toast.makeText ( MainActivity.this, t.getMessage (), Toast.LENGTH_LONG ).show ();

            }
        } );


    }
}

我还将清单文件更改为包含internet permission。我用它Vysor来测试。

提问于
用户回答回答于

你已经提供了

student终点BASE_URL。改变你的BASE_URL偏好:

private static final String BASE_URL = "192.168.2.23/api/";

用户回答回答于

试着把

Route::post('student/store', 'StudentsApiController@store');

改成

Route::post('/store', 'StudentsApiController@store');

扫码关注云+社区

领取腾讯云代金券