How can I get token with Retrofit2?

2019-09-20 07:16发布

问题:

I have a web service link for token login. But in this link, there is no "/" at the end of link. And android studio make error called baseUrl must end in /. When I put / it don't get token and say token is not truth :( because the link is not correct in my think. I use retrofit2library. Please help me to solve it.

MainActivity.java

public class MainActivity extends AppCompatActivity {

  Retrofit.Builder builder = new Retrofit.Builder()
    .baseUrl("http://website.net/token")
    .addConverterFactory(GsonConverterFactory.create());

Retrofit retrofit = builder.build();
UserClient userClient = retrofit.create(UserClient.class);

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


    Button loginButton=(Button)findViewById(R.id.btn_login);
    loginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            login();
           /* Intent intentLogin=new Intent(MainActivity.this,MainPageActivity.class);
            startActivity(intentLogin);*/
        }
    });



}
private static String token;
private void login() {
    Login login = new Login("abcd", "1234");
    Call<User> call = userClient.login(login);

    call.enqueue(new Callback<User>() {
        @Override
        public void onResponse(Call<User> call, Response<User> response) {
            if (response.isSuccessful()){
                Toast.makeText(MainActivity.this, response.body().getToken(), Toast.LENGTH_SHORT).show();
                token = response.body().getToken();
            }
            else {
                Toast.makeText(MainActivity.this, "Token is not truth :(", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure(Call<User> call, Throwable t) {
            Toast.makeText(MainActivity.this, "error!", Toast.LENGTH_SHORT).show();
        }
    });
}
}

   `Login.java

     public class Login {
private String user;
private String password;

public Login(String user, String password) {
    this.user = user;
    this.password = password;
}
}

 User.java

 public class User {
private int id;
private String email;
private String token;

public int getId(){
    return id;
}

public void setId(){
    this.id = id;
}
public String getEmail(){
    return  email;
}

public void setEmail(String email){
    this.email = email;
}
public String getToken(){return token;}
public void setToken(String token){this.token = token;}
}


 UserClient.java

 import com.squareup.okhttp.ResponseBody;

 import retrofit2.Call;
 import retrofit2.http.Body;
 import retrofit2.http.GET;
 import retrofit2.http.Header;
  import retrofit2.http.POST;

 public interface UserClient {
@POST("Login")
Call<User> login(@Body Login login);

//    @GET("secretinfo")
//    Call<ResponseBody> getSecret(@Header("Authorization") String authToken);
}

回答1:

Use upto this in

Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl("http://website.net/")
.addConverterFactory(GsonConverterFactory.create());

and in the UserClient interface class

public interface UserClient { 
@POST("token") Call<User> login(@Body Login login); 
}