2017-08-05 82 views
-3

我正在开发一款具有改进功能的应用程序。
我是新开发的改造登录。如何通过改造实施登录?

的登录我的JSON代码:

{ 
     "login": [ 
      { 
       "sessionid": 12973, 
       "responsetypes": "success" 
      } 
     ] 
    } 

如何实现这种类型的JSON的登录? 以下是我的代码:它给错误的反应method.how我解决这个问题?

公共类LoginActivity延伸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; 
} 

}

//公共类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; 
} 

}

//

公共类登录{

@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

分享您试图代码.... –

+0

我加入了代码,请建议我? – Rohu

+0

请发布ServerResponse的代码 –

回答

0

看你的代码后,请参阅该方法的步骤2,改变你的ServerResponse类。您没有正确映射响应。

你需要采取以下步骤来实现任何类型的API的改造:

  1. 将界面定义您的最终其中包括响应和请求。

    public interface MyClient { 
    @FromUrlEncoded 
    @POST("your/login/end/point/") 
    Call<RequestClass> login(@String("username") String username, other post parameters); 
    } 
    
  2. 作出回应POJO类。这将是我上面提到的RequestClass。您可以从http://www.jsonschema2pojo.org/创建它。

  3. 做一个REST客户端 你可以让REST客户端,为您的API如下:

    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); 
    
  4. 应用它,你想发送请求到服务器。

    // 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 
    } 
    }); 
    
+0

逐步完成本指南,您将能够非常轻松地实现您的代码 –

+0

请接受适合您的答案! :) –

+0

现在我编辑了这个问题,我已经添加了回复,请告诉我如何登录? – Rohu