Retrofit2登录

时间:2016-11-22 18:14:14

标签: java android rest api

我正在尝试将我的登录信息转换为retrofit2我的旧LoginActivity:

public class LoginActivity extends Activity {
private static final String TAG = RegisterActivity.class.getSimpleName();

// GPSTracker class
GPSTracker gps;

private Button btnLogin;
private Button btnLinkToRegister;
private EditText inputEmail;
private EditText inputPassword;
private ProgressDialog pDialog;
private SessionManager session;
private SQLiteHandler db;

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

    inputEmail = (EditText) findViewById(R.id.email);
    inputPassword = (EditText) findViewById(R.id.password);
    btnLogin = (Button) findViewById(R.id.btnLogin);
    btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);

    // Progress dialog
    pDialog = new ProgressDialog(this);
    pDialog.setCancelable(false);

    // SQLite database handler
    db = new SQLiteHandler(getApplicationContext());

    // Session manager
    session = new SessionManager(getApplicationContext());

    // Check if user is already logged in or not
    if (session.isLoggedIn()) {
        // User is already logged in. Take him to main activity
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    // Login button Click Event
    btnLogin.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            String email = inputEmail.getText().toString().trim();
            String password = inputPassword.getText().toString().trim();

            // Check for empty data in the form
            if (!email.isEmpty() && !password.isEmpty()) {
                // login user
                checkLogin(email, password);
            } else {
                // Prompt user to enter credentials
                Toast.makeText(getApplicationContext(),
                        R.string.login_pleaseentercredentials, Toast.LENGTH_LONG)
                        .show();
            }
        }

    });

    // Link to Register Screen
    btnLinkToRegister.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            Intent i = new Intent(getApplicationContext(),
                    RegisterActivity.class);
            startActivity(i);
            finish();
        }
    });

}

/**
 * function to verify login details in mysql db
 * */
private void checkLogin(final String email, final String password) {
    // Tag used to cancel the request
    String tag_string_req = "req_login";

    pDialog.setMessage(getResources().getString(R.string.login_txt_loggingin));
    showDialog();

    StringRequest strReq = new StringRequest(Method.POST,
            AppConfig.API_URL + "/login", new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            Log.d(TAG, "Login Response: " + response.toString());
            hideDialog();

            try {
                JSONObject jObj = new JSONObject(response);
                boolean error = jObj.getBoolean("error");

                // Check for error node in json
                if (!error) {
                    // user successfully logged in
                    // Create login session

                    // Now store the user in SQLite
                    String uid = jObj.getString("uid");

                    String name = jObj.getString("name");
                    String email = jObj.getString("email");
                    String created_at = jObj.getString("created_at");
                    String apiKey = jObj.getString("apiKey");
                    String status = jObj.getString("status");

                    if(status.equals("1")) {
                        // Launch main activity
                        Intent intent = new Intent(LoginActivity.this, UserBlockedActivity.class);
                        startActivity(intent);
                        finish();
                    } else {
                        session.setLogin(true);

                        // Inserting row in users table
                        db.addUser(name, email, uid, created_at, apiKey);

                        // Launch main activity
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(intent);
                        finish();
                    }
                } else {
                    // Error in login. Get the error message
                    String errorMsg = jObj.getString("message");

                    if (errorMsg.equals("login_incorrectcredentials")) {
                        Toast.makeText(getApplicationContext(), R.string.login_incorrectcredentials, Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(getApplicationContext(), errorMsg, Toast.LENGTH_LONG).show();
                    }
                }
            } catch (JSONException e) {
                // JSON error
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
            }

        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Login Error: " + error.getMessage());
            Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
            hideDialog();
        }
    }) {

        @Override
        protected Map<String, String> getParams() {
            // Posting parameters to login url
            Map<String, String> params = new HashMap<String, String>();
            params.put("email", email);
            params.put("password", password);

            return params;
        }

    };

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}

private void showDialog() {
    if (!pDialog.isShowing())
        pDialog.show();
}

private void hideDialog() {
    if (pDialog.isShowing())
        pDialog.dismiss();
}
}

我的新LoginActivity

public class LoginActivity extends AppCompatActivity {

private static final String TAG = LoginActivity.class.getSimpleName();

private EditText inputEmail;
private EditText inputPassword;

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

    inputEmail = (EditText) findViewById(R.id.email);
    inputPassword = (EditText) findViewById(R.id.password);
}

// When Button Login clicked
public void Signin(View v) {

    String email = inputEmail.getText().toString().trim();
    String password = inputPassword.getText().toString().trim();

    //Calling method of field validation
    if (CheckFieldValidation()) {

        ApiInterface apiService =
                RestClient.getClient().create(ApiInterface.class);

        HashMap<String, String> parameters = new HashMap<>();
        parameters.put("email", email);
        parameters.put("password", password);

        //Calling method to get check login

        Call<LoginModal> call = apiService.Login(parameters);
        call.enqueue(new Callback<LoginModal>() {
            @Override
            public void onResponse(Call<LoginModal> call, Response<LoginModal> response) {
                if (response.body().getStatus().equals("1")) {
                    Toast.makeText(LoginActivity.this, "Logged in", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(LoginActivity.this, "Invalid UserName/Pass ", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<LoginModal> call, Throwable t) {
                // something went completely south (like no internet connection)
                Toast.makeText(LoginActivity.this, "An error occurred. Please try again", Toast.LENGTH_SHORT).show();
                Log.d("Error", t.getMessage());
            }
        });

    }
}

//checking field are empty
private boolean CheckFieldValidation() {

    boolean valid = true;
    if (inputEmail.getText().toString().equals("")) {
        inputEmail.setError("Can't be Empty");
        valid = false;
    } else if (inputPassword.getText().toString().equals("")) {
        inputPassword.setError("Can't be Empty");
        valid = false;
    }

    return valid;

}
}

我的RestClient看起来像这样:

public class RestClient {

public static final String BASE_URL = "http://api.fitnessm8.dk/v2/";
private static Retrofit retrofit = null;

public static Retrofit getClient() {
    if (retrofit==null) {

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(interceptor)
                //.addInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
                .addNetworkInterceptor(new Interceptor() {

                    @Override

                    public okhttp3.Response intercept(Chain chain) throws IOException {
                        Request request = chain.request().newBuilder()
                                // .addHeader(Constant.Header, authToken)
                                .build();
                        return chain.proceed(request);
                    }
                }).build();

        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();
    }
    return retrofit;
    }
}

ApiInterface.java

public interface ApiInterface {
    @FormUrlEncoded
    @POST("login")
    Call<LoginModal> Login(@Body HashMap<String, String> parameters);
}

LoginModal.java

public class LoginModal {

@SerializedName("id")
@Expose
private String id;
@SerializedName("fullname")
@Expose
private String fullname;
@SerializedName("email")
@Expose
private String email;
@SerializedName("api_key")
@Expose
private String apiKey;
@SerializedName("status")
@Expose
private String status;
@SerializedName("created_at")
@Expose
private String createdAt;

/**
 *
 * @return
 * The id
 */
public String getId() {
    return id;
}

/**
 *
 * @param id
 * The id
 */
public void setId(String id) {
    this.id = id;
}

/**
 *
 * @return
 * The fullname
 */
public String getFullname() {
    return fullname;
}

/**
 *
 * @param fullname
 * The fullname
 */
public void setFullname(String fullname) {
    this.fullname = fullname;
}

/**
 *
 * @return
 * The email
 */
public String getEmail() {
    return email;
}

/**
 *
 * @param email
 * The email
 */
public void setEmail(String email) {
    this.email = email;
}

/**
 *
 * @return
 * The apiKey
 */
public String getApiKey() {
    return apiKey;
}

/**
 *
 * @param apiKey
 * The api_key
 */
public void setApiKey(String apiKey) {
    this.apiKey = apiKey;
}

/**
 *
 * @return
 * The status
 */
public String getStatus() {
    return status;
}

/**
 *
 * @param status
 * The status
 */
public void setStatus(String status) {
    this.status = status;
}

/**
 *
 * @return
 * The createdAt
 */
public String getCreatedAt() {
    return createdAt;
}

/**
 *
 * @param createdAt
 * The created_at
 */
public void setCreatedAt(String createdAt) {
    this.createdAt = createdAt;
}
}

然后我尝试登录应用程序时收到错误:

引起:java.lang.IllegalArgumentException:@Body参数不能与表单或多部分编码一起使用。 (参数#1)

我是Android开发的新手,所以我的错误在于ApiInterface与Body,但我如何让它工作? :)

我正在使用来自androidhive的Rest Api,这段代码但我修改了它,以适合我的应用程序:

http://www.androidhive.info/2014/01/how-to-create-rest-api-for-android-app-using-php-slim-and-mysql-day-12-2/

1 个答案:

答案 0 :(得分:2)

当你添加@FormUrlEncoded时,数据以param1 = value1&amp; param2 = value2的形式发送,即表格编码。使用@Body时,参数将转换为JSON并发送。根据您的服务器端代码,您需要选择一个,而不是两个。