Java 使用查询参数改装URL
我第一次尝试改型,但缺少简单的逻辑。请帮我解决这个问题 这是我的用户类Java 使用查询参数改装URL,java,android,json,retrofit2,Java,Android,Json,Retrofit2,我第一次尝试改型,但缺少简单的逻辑。请帮我解决这个问题 这是我的用户类 public class User { private String name, email, password; public User(){ } public User(String name, String email, String password){ this.name = name; this.email = email; t
public class User {
private String name, email, password;
public User(){
}
public User(String name, String email, String password){
this.name = name;
this.email = email;
this.password = password;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public String getPassword() {
return password;
}
public void setName(String name) {
this.name = name;
}
public void setEmail(String email) {
this.email = email;
}
public void setPassword(String password) {
this.password = password;
}
}
API接口
public interface MyApiEndpointInterface {
// Request method and URL specified in the annotation
// Callback for the parsed response is the last parameter
@GET("users?email={email}")
Call<User> getUser(@Query("email") String email);
}
公共接口MyApidentInterface{
//注释中指定的请求方法和URL
//已解析响应的回调是最后一个参数
@获取(“用户?电子邮件={email}”)
调用getUser(@Query(“email”)字符串email);
}
以下是我获取详细信息的方式:
public void getUserDetails()
{
String email = inputEmail.getText().toString()
Call<User> call = apiService.getUser(email);
call.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User>call, Response<User> response) {
if(response.body()!=null)
{
Log.d("TAG", "Name: " + response.body().getName());
Log.d("TAG", "Password: " + response.body().getPassword());
}
else
{
Toast.makeText(getApplicationContext(), "User does not exist", Toast.LENGTH_SHORT).show();
Log.d("TAG", "User details does not exist");
}
}
@Override
public void onFailure(Call<User>call, Throwable t) {
Log.e("TAG", t.toString());
}
});
}
public void getUserDetails()
{
字符串email=inputEmail.getText().toString()
Call Call=apiService.getUser(电子邮件);
call.enqueue(新回调(){
@凌驾
public void onResponse(Callcall、Response-Response){
if(response.body()!=null)
{
Log.d(“标记”,“名称:”+response.body().getName());
Log.d(“TAG”,“Password:+response.body().getPassword());
}
其他的
{
Toast.makeText(getApplicationContext(),“用户不存在”,Toast.LENGTH\u SHORT.show();
Log.d(“标签”,“用户详细信息不存在”);
}
}
@凌驾
失败时公共无效(Callcall、Throwable t){
Log.e(“TAG”,t.toString());
}
});
}
现在我的问题是我有一个web api,它托管在服务器上,看起来像:
要根据提供的电子邮件获取用户详细信息,我尝试使用以下方法:
{email}
现在,我如何在api接口中将此url设置为返回null?在
apiService
中,您应该使用生成器创建一个Reformation
对象,并使用该对象创建MyApiEndpointInterface
接口的实例。在这里添加API的baseUrl
应该是这样的:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://yourapibaseUrl.com/api/")
.build();
MyApiEndpointInterface apiInterface = retrofit.create(MyApiEndpointInterface.class);
apinterface
是您将使用Refit调用API的对象,并且它已经设置了baseUrl
希望这能有所帮助。-啊,傻……刚才我也看到了,因为它不见了……谢谢你,伙计:)