首先需要SHAI加密你的AppId和AppKey,规则如下:
your app key = SHA1(你的应用ID + ‘UZ’ + 你的应用KEY +’UZ’ + 当前时间毫秒数).当前时间毫秒数
我们的加密Code,参考Android 实现SHA1加密算法代码这篇文章。
登录方法:
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
/**
* Created by zhi.zhang on 12/22/15.
*/
public class UserNetworkRequest {
private Context mContext;
RequestQueue mRequestQueue;
LoginLisenter mLoginLisenter;
RegisterLisenter mRegisterLisenter;
public interface LoginLisenter {
void loginSuccess(String token);
}
public interface RegisterLisenter {
void registerSuccess(JSONObject jsonObject);
void registerFail();
}
public void setLoginLisenter(LoginLisenter loginLisenter) {
mLoginLisenter = loginLisenter;
}
public void setRegisterLisenter(RegisterLisenter registerLisenter) {
mRegisterLisenter = registerLisenter;
}
public UserNetworkRequest(Context context) {
mContext = context;
mRequestQueue = Volley.newRequestQueue(mContext);
}
private Map<String, String> getHeaderMap() {
Map<String, String> header = new HashMap<>();
header.put("X-APICloud-AppId", "你的AppId");
header.put("X-APICloud-AppKey", getSecurityAppKey());
header.put("Content-Type", "application/json");
return header;
}
public void userLogin(String name, String password) {
String LoginUrl = "https://d.apicloud.com" + "/mcm/api/user/login";
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("username", name);
jsonObject.put("password", password);
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, LoginUrl,
jsonObject, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
Toast toast = Toast.makeText(mContext, jsonObject.toString(), Toast.LENGTH_SHORT);
toast.show();
try {
String Token = jsonObject.getString("id");
if (Token != null) {
mLoginLisenter.loginSuccess(Token);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return getHeaderMap();
}
};
mRequestQueue.add(jsonObjectRequest);
}
public void registeNewUser(Map map) {
String registeUrl = "https://d.apicloud.com" + "/mcm/api/user";
JSONObject jsonObject = new JSONObject(map);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, registeUrl,
jsonObject, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
Toast toast = Toast.makeText(mContext, jsonObject.toString(), Toast.LENGTH_SHORT);
toast.show();
mRegisterLisenter.registerSuccess(jsonObject);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
mRegisterLisenter.registerFail();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return getHeaderMap();
}
};
mRequestQueue.add(jsonObjectRequest);
}
public void userLogout(final String token) {
String logoutUrl = "https://d.apicloud.com" + "/mcm/api/user/logout";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, logoutUrl,
null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
Toast toast = Toast.makeText(mContext, jsonObject.toString(), Toast.LENGTH_SHORT);
toast.show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> header = new HashMap<>();
header.put("X-APICloud-AppId", "你的AppId");
header.put("X-APICloud-AppKey", getSecurityAppKey());
header.put("authorization", "登录成功或注册成功返回来的ID");
header.put("Content-Type", "application/json");
return header;
}
};
mRequestQueue.add(jsonObjectRequest);
}
}