我正在开发一个改装的应用程序 我不熟悉使用改造开发登录。
我登录的JSON代码:
{
"login": [
{
"sessionid": 12973,
"responsetypes": "success"
}
]
}
如何实现此类JSON的登录? 以下是我的代码:它给响应方法带来了错误。我能解决这个问题吗?
public class LoginActivity extends AppCompatActivity实现了View.OnClickListener {
private AppCompatButton btn_login;
private EditText et_email,et_password;
private TextView tv_register;
private ProgressBar progress;
private SharedPreferences pref;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
pref = LoginActivity.this.getPreferences(0);
btn_login = (AppCompatButton)findViewById(R.id.btn_login);
tv_register = (TextView)findViewById(R.id.tv_register);
et_email = (EditText)findViewById(R.id.et_email);
et_password = (EditText)findViewById(R.id.et_password);
progress = (ProgressBar)findViewById(R.id.progress);
btn_login.setOnClickListener(this);
tv_register.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.tv_register:
goToRegister();
break;
case R.id.btn_login:
String email = et_email.getText().toString();
String password = et_password.getText().toString();
if(!email.isEmpty() && !password.isEmpty()) {
progress.setVisibility(View.VISIBLE);
loginProcess(email,password);
} else {
Toast.makeText(LoginActivity.this, "Fields are empty !", Toast.LENGTH_LONG).show();
}
break;
}
}
private void loginProcess(String userID,String password){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
RequestInterface requestInterface = retrofit.create(RequestInterface.class);
User user = new User();
user.setEmail(userID);
user.setPassword(password);
ServerRequest request = new ServerRequest();
request.setOperation(Constants.LOGIN_OPERATION);
request.setUser(user);
Call<ServerResponse> response = requestInterface.operation(request);
response.enqueue(new Callback<ServerResponse>() {
@Override
public void onResponse(Call<ServerResponse> call, retrofit2.Response<ServerResponse> response) {
ServerResponse resp = response.body();
Toast.makeText(LoginActivity.this, resp.getMessage(), Toast.LENGTH_LONG).show();
if(resp.getResult().equals(Constants.SUCCESS)){
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean(Constants.IS_LOGGED_IN,true);
editor.putString(Constants.EMAIL,resp.getUser().getEmail());
// editor.putString(Constants.NAME,resp.getUser().getName());
editor.putString(Constants.UNIQUE_ID,resp.getUser().getUnique_id());
editor.apply();
goToProfile();
}
progress.setVisibility(View.INVISIBLE);
}
@Override
public void onFailure(Call<ServerResponse> call, Throwable t) {
progress.setVisibility(View.INVISIBLE);
Log.d(Constants.TAG,"failed");
//Snackbar.make(getV, t.getLocalizedMessage(), Snackbar.LENGTH_LONG).show();
}
});
}
private void goToRegister(){
Intent i = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(i);
}
private void goToProfile(){
Intent i = new Intent(LoginActivity.this, MainActivity.class);
startActivity(i);
}
}
// 公共接口RequestInterface {
@POST("ypAndroid/api/")
Call<ServerResponse> operation(@Body ServerRequest request);
}
公共类常量{
public static final String BASE_URL = "http://www.example.com/";
public static final String REG_URL = "http://www.example.com/";
public static final String REGISTER_OPERATION = "register";
public static final String LOGIN_OPERATION = "login";
public static final String CHANGE_PASSWORD_OPERATION = "chgPass";
public static final String SUCCESS = "success";
public static final String FAILURE = "failure";
public static final String IS_LOGGED_IN = "isLoggedIn";
public static final String NAME = "name";
public static final String EMAIL = "userID";
public static final String UNIQUE_ID = "sessionid";
public static final String TAG = "Demo";
}
// 公共类ServerRequest {
private String operation;
private User user;
public void setOperation(String operation) {
this.operation = operation;
}
public void setUser(User user) {
this.user = user;
}
}
// public class ServerResponse {
private String result;
private String message;
private User user;
public String getResult() {
return result;
}
public String getMessage() {
return message;
}
public User getUser() {
return user;
}
}
//
public class Login {
@SerializedName("sessionid")
@Expose
private Integer sessionid;
@SerializedName("responsetypes")
@Expose
private String responsetypes;
public Integer getSessionid() {
return sessionid;
}
public void setSessionid(Integer sessionid) {
this.sessionid = sessionid;
}
public String getResponsetypes() {
return responsetypes;
}
public void setResponsetypes(String responsetypes) {
this.responsetypes = responsetypes;
}
}
答案 0 :(得分:1)
查看代码后,请参阅流程的第2步并更改ServerResponse类。您没有正确映射响应。
您需要采取以下步骤在改造中实施任何类型的API:
创建一个定义结束的界面,其中包括响应和请求。
public interface MyClient {
@FromUrlEncoded
@POST("your/login/end/point/")
Call<RequestClass> login(@String("username") String username, other post parameters);
}
回复POJO班级。它将是我上面提到的RequestClass。您可以从http://www.jsonschema2pojo.org/创建它。
制作REST客户端 您可以按如下方式为您的API创建Rest客户端:
String API_BASE_URL = "https://your.base.url/";
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(
GsonConverterFactory.create()
);
Retrofit retrofit = builder.client(httpClient.build()).build();
RestClient client = retrofit.create(MyClient.class);
将其应用于您要将请求发送到服务器的位置。
// Create a very simple REST adapter which points the GitHub API endpoint.
RestClient client = retrofit.create(GitHubClient.class);
// Call the API
Call<ResponseClass> call = client.login(parameter_of_login_call);
call.enqueue(new Callback<ResponseClass>() {
@Override
public void onResponse(Call<ResponseClass> call, Response<ResponseClass> response) {
// The network call was a success and we got a response
// implement the what needs to be done here
}
@Override
public void onFailure(Call<ResponseClass> call, Throwable t) {
// the network call was a failure
// handle error
}
});