1

I've been losing my mind trying to figure out why the end android retrofit is not sending the POST. Here is what I've done. In Laravel I've set up the model, controller, changed the VerifyCsrfToken to allow api. I then set the routes web and api. All was tested through postmen and it works, yet when I try to post through android the code fails.

Here are my routs for Laravel

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 ();

            }
        } );


    }
}

I also changed the manifest file to include internet permission. And I use Vysor for testing.

Thelouras
  • 852
  • 1
  • 10
  • 30
Simona Buga
  • 341
  • 6
  • 22

3 Answers3

2

Base url in retrofit will delete every thing after first / so your base url should contain just your ip and in your android api file @post('store') Should be @post('api/student/store') base_url ="http://192.168.1.6" just for example

Mohammed Aktaa
  • 1,345
  • 9
  • 14
0

Try to change this

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

to

Route::post('/store', 'StudentsApiController@store');
Ashish
  • 6,791
  • 3
  • 26
  • 48
0

You already provided

student endpoint in BASE_URL. change your BASE_URL like this:

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

WatchMyApps Lab
  • 113
  • 1
  • 7