Android 使用APICloude 数据库的方法

首先需要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);
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值