Skip to content

Instantly share code, notes, and snippets.

@koleksiuk
Created January 7, 2014 21:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koleksiuk/8307289 to your computer and use it in GitHub Desktop.
Save koleksiuk/8307289 to your computer and use it in GitHub Desktop.
package com.ocelotty.memestream.Services;
import android.app.Activity;
import android.content.Context;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonObjectRequest;
import com.ocelotty.memestream.MainActivity;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.UnsupportedEncodingException;
public abstract class ApiRequest<T> {
protected Context context;
protected T activity;
final String url = "http://memestream.herokuapp.com";
final protected String client_id = "2a571d63f685bd2a9e7e82af192651692848b0e0ec19ae6b05194dd99ffa1500";
final protected String client_secret = "78a0d630a90dd03dcf50f0b49b7b16894f820d864fc5b6a6ac3c0ce28857bfd6";
final String api_path = "/api/v1";
JsonObjectRequest request;
protected String requestURI(String path) {
return url + api_path + path;
}
protected JsonObjectRequest postRequest(JSONObject params, String uri) {
request = new JsonObjectRequest(requestURI(uri), params,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
handleRequest(response);
VolleyLog.v("VOLLEY Response:%n %s", response.toString(4));
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
String strRespons = new String(error.networkResponse.data);
JSONObject errorObject;
try {
errorObject = new JSONObject(strRespons);
} catch (JSONException e) {
e.printStackTrace();
}
handleErrorRequest(VolleyErrorHelper.getMessage(error, context));
}
}
);
return request;
}
protected JsonObjectRequest getRequest(String uri) {
request = new JsonObjectRequest(requestURI(uri), null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
VolleyLog.v("Response:%n %s", response.toString(4));
handleRequest(response);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("Error: %n %s", error.getMessage());
String errorRes = VolleyErrorHelper.getMessage(error, context);
handleErrorRequest(errorRes);
}
}
);
return request;
}
protected void handleRequest(JSONObject response) {
}
protected void handleErrorRequest(String response) {
}
}
public class RegistrationActivity extends Activity implements UserRegistrationDelegator {
public onClick(costam) {
try
{
userRegistration = new UserRegistration(mEmail,mPassword,mPasswordConfirmation);
ApplicationController.getInstance().addToRequestQueue(userRegistration.call(this));
}
}
public void onUserRegistered(User user) {
Intent k = new Intent(ApplicationController.getInstance().getApplicationContext(), MainActivity.class);
startActivity(k);
finish();
}
public void onUserRegistrationError(String error) {
showProgress(false);
userRegistration = null;
Toast.makeText(getApplicationContext(), error,
Toast.LENGTH_LONG).show();
}
}
package com.ocelotty.memestream.Services.Api;
import android.app.Activity;
import android.util.Log;
import com.android.volley.toolbox.JsonObjectRequest;
import com.ocelotty.memestream.MainActivity;
import com.ocelotty.memestream.Services.Api.Delegators.UserRegistrationDelegator;
import com.ocelotty.memestream.Services.ApiRequest;
import org.json.JSONException;
import org.json.JSONObject;
import com.ocelotty.memestream.Models.User;
/* Allows user registration. Returns QueueRequest to handle
*
* Example usage:
* UserRegistration userRegistration = new UserRegistration("foo@bar.com", "pass", "pass");
*
* ApplicationController.getInstance().addToRequestQueue(userRegistration.call(this));
* this - Activity (implements UserRegistrationDelegator)
*/
public class UserRegistration<T extends UserRegistrationDelegator> extends ApiRequest {
public static class UnmatchedPasswordsException extends Exception {
public UnmatchedPasswordsException(String s) {
super(s);
}
}
public String email, password, passwordConfirmation;
private User user = null;
public JsonObjectRequest request;
/*
* @param [String] email
* @param [String] password
* @param [String] passwordConfirmation
*
* Throws UnmatchedPasswordsException if password does not match confirmation
*/
public UserRegistration(String email, String password, String passwordConfirmation) throws UnmatchedPasswordsException {
this.email = email;
this.password = password;
this.passwordConfirmation = passwordConfirmation;
checkPasswords();
}
public JsonObjectRequest call(T activity) {
this.activity = activity;
return postRequest(userParams(), "/users.json");
}
private void checkPasswords() throws UnmatchedPasswordsException {
if (!password.equals(passwordConfirmation)) {
Log.d("UserRegistration", "Passwords did not match");
throw new UnmatchedPasswordsException("MEME_STREAM_PASSWORD_UNMATCHED");
}
}
protected JSONObject userParams() {
JSONObject user_params = new JSONObject();
// Temp variable
JSONObject payload = new JSONObject();
try {
payload.put("email", email);
payload.put("password", password);
payload.put("password_confirmation", passwordConfirmation);
user_params.put("user", payload);
} catch (JSONException e) {
e.printStackTrace();
}
return user_params;
}
protected void handleRequest(JSONObject response) {
long id = 0;
String email = null;
try {
id = response.getLong("id");
email = response.getString("email");
} catch (JSONException e) {
e.printStackTrace();
}
User user = new User(id, email);
T registrationActivity = (T) activity;
registrationActivity.onUserRegistered(user);
}
protected void handleErrorRequest(String response) {
T registrationActivity = (T) activity;
registrationActivity.onUserRegistrationError(response);
}
}
package com.ocelotty.memestream.Services.Api.Delegators;
import com.ocelotty.memestream.Models.User;
/**
* Created by koleksiuk on 06/01/14.
*/
public interface UserRegistrationDelegator {
void onUserRegistered(User user);
void onUserRegistrationError(String response);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment