Search in sources :

Example 1 with ResultKeyGsonBean

use of com.mtm.mrecord.mvp.model.entity.ResultKeyGsonBean in project Mvp-Rxjava-Retrofit-dagger2 by pengMaster.

the class SplashPresenter method toActDownLoadNetKey.

/**
 * 下载网络请求验证码
 */
private void toActDownLoadNetKey() {
    ThreadUtils.runThread(new Runnable() {

        @Override
        public void run() {
            HttpClient httpClient = new HttpClient();
            Map<String, String> msg = new HashMap<String, String>();
            msg.put("appKey", "9c3d77f18e4848d095e626e9b3a009a3");
            msg.put("appSecret", "ff265c879c4ac08028e77a6c66078f9ce81c15b6fbc76c18f7a12a97c859c92b");
            String params = new Gson().toJson(msg);
            StringRequestEntity paramEntity = null;
            try {
                paramEntity = new StringRequestEntity(params, "application/json", "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            PostMethod postMethod = new PostMethod(Api.POST_KEY);
            postMethod.setRequestEntity(paramEntity);
            try {
                httpClient.executeMethod(postMethod);
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                InputStream inputStream = postMethod.getResponseBodyAsStream();
                final String resultKey = convertStreamToString(inputStream);
                ThreadUtils.runInMainThread(new Runnable() {

                    @Override
                    public void run() {
                        ResultKeyGsonBean resultKeyBean = new Gson().fromJson(resultKey, ResultKeyGsonBean.class);
                        String access_token = resultKeyBean.getData().getAccess_token();
                        GlobalConstantUtils.setToken(access_token);
                        Observable.create(new ObservableOnSubscribe<String>() {

                            @Override
                            public void subscribe(@NonNull ObservableEmitter<String> e) throws Exception {
                                SystemClock.sleep(2000);
                                e.onNext("");
                                e.onComplete();
                            }
                        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<String>() {

                            @Override
                            public void accept(String s) throws Exception {
                                Intent intent = new Intent(AppLifecyclesImpl.mAppContext, MainActivity.class);
                                mRootView.launchActivity(intent);
                                mRootView.killMyself();
                            }
                        });
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) PostMethod(org.apache.commons.httpclient.methods.PostMethod) InputStream(java.io.InputStream) Gson(com.google.gson.Gson) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Intent(android.content.Intent) IOException(java.io.IOException) MainActivity(com.mtm.mrecord.mvp.ui.activity.MainActivity) ResultKeyGsonBean(com.mtm.mrecord.mvp.model.entity.ResultKeyGsonBean) LoginException(javax.security.auth.login.LoginException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) HttpClient(org.apache.commons.httpclient.HttpClient) NonNull(io.reactivex.annotations.NonNull) Map(java.util.Map) HashMap(java.util.HashMap) ObservableEmitter(io.reactivex.ObservableEmitter)

Aggregations

Intent (android.content.Intent)1 Gson (com.google.gson.Gson)1 ResultKeyGsonBean (com.mtm.mrecord.mvp.model.entity.ResultKeyGsonBean)1 MainActivity (com.mtm.mrecord.mvp.ui.activity.MainActivity)1 ObservableEmitter (io.reactivex.ObservableEmitter)1 ObservableOnSubscribe (io.reactivex.ObservableOnSubscribe)1 NonNull (io.reactivex.annotations.NonNull)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 LoginException (javax.security.auth.login.LoginException)1 HttpClient (org.apache.commons.httpclient.HttpClient)1 PostMethod (org.apache.commons.httpclient.methods.PostMethod)1 StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)1