本文介绍了如何使用Retrofit2获得代币?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个用于令牌登录的Web服务链接.但是在此链接中,链接末尾没有"/".和android studio使称为baseUrl must end in /
的错误.当我放入/
时,它不会获得令牌并说出token is not truth :(
,因为我认为该链接不正确.我使用retrofit2
库.请帮我解决.
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 retrofit2
library. 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);
}
推荐答案
在
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl("http://website.net/")
.addConverterFactory(GsonConverterFactory.create());
以及在UserClient界面类中
and in the UserClient interface class
public interface UserClient {
@POST("token") Call<User> login(@Body Login login);
}
这篇关于如何使用Retrofit2获得代币?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!