Java Code Examples for com.lzy.okgo.model.Response

The following examples show how to use com.lzy.okgo.model.Response. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: DevUtils   Source File: OkGoCallback.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 请求失败, 响应错误, 数据解析错误等, 都会回调该方法,  UI 线程
 */
@Override
public void onError(Response<String> response) {
    super.onError(response);

    DevLogger.dTag(TAG, "请求失败: " + url);

    if (response != null) {
        onErrorResponse(
                new OkGoResponse.Builder<T>()
                        .setCode(response.code() + "")
                        .setMessage(response.message())
                        .setOriginal(response.body())
                        .setException(response.getException())
                        .build()
        );
    } else {
        onErrorResponse(
                new OkGoResponse.Builder<T>()
                        .setCode(Integer.MAX_VALUE + "")
                        .setMessage("response is null")
                        .setException(new OkGoException("response is null"))
                        .build()
        );
    }
}
 
Example 2
Source Project: BaseProject   Source File: NoneCacheRequestPolicy.java    License: Apache License 2.0 6 votes vote down vote up
@Override
    public void onError(final Response<T> error) {
        if (mCallback != null) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    callbackOnError(error);
                    callbackOnFinish();
//                    mCallback.onError(error);
//                    mCallback.onFinish();
                }
            });
        }
//        runOnUiThread(new Runnable() {
//            @Override
//            public void run() {
//                mCallback.onError(error);
//                mCallback.onFinish();
//            }
//        });
    }
 
Example 3
public void sendRedPacketData(String account, String amount, String packetCount, String type) {
    HashMap<String, String> hashMap = new HashMap<String, String>();
    hashMap.put("uid", MyApplication.getInstance().getUserBean().getWallet_uid());
    hashMap.put("account", account);
    hashMap.put("amount", amount);
    hashMap.put("packetCount", packetCount);
    hashMap.put("type", type);//资产类型 0 oct 1 eos 2 其他
    HttpUtils.postRequest(BaseUrl.HTTP_send_red_packet, mContext, hashMap, new JsonCallback<ResponseBean<SendRedPacketBean.DataBean>>() {
        @Override
        public void onSuccess(Response<ResponseBean<SendRedPacketBean.DataBean>> response) {
            if (response.body().code == 0) {
                view.sendRedPacketDataHttp(response.body().data);
            } else {
                view.getDataHttpFail(response.body().message);
            }
        }
    });
}
 
Example 4
Source Project: okhttp-OkGo   Source File: NoneCacheRequestPolicy.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void requestAsync(final CacheEntity<T> cacheEntity, Callback<T> callback) {
    mCallback = callback;
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            mCallback.onStart(request);

            try {
                prepareRawCall();
            } catch (Throwable throwable) {
                Response<T> error = Response.error(false, rawCall, null, throwable);
                mCallback.onError(error);
                return;
            }
            if (cacheEntity != null) {
                Response<T> success = Response.success(true, cacheEntity.getData(), rawCall, null);
                mCallback.onCacheSuccess(success);
                mCallback.onFinish();
                return;
            }
            requestNetworkAsync();
        }
    });
}
 
Example 5
Source Project: BaseProject   Source File: DefaultCachePolicy.java    License: Apache License 2.0 6 votes vote down vote up
@Override
    public void onError(final Response<T> error) {
        if (mCallback != null) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    callbackOnError(error);
                    callbackOnFinish();
//                    mCallback.onError(error);
//                    mCallback.onFinish();
                }
            });
        }
//        runOnUiThread(new Runnable() {
//            @Override
//            public void run() {
//                mCallback.onError(error);
//                mCallback.onFinish();
//            }
//        });
    }
 
Example 6
Source Project: okhttp-OkGo   Source File: DefaultCachePolicy.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean onAnalysisResponse(final Call call, final okhttp3.Response response) {
    if (response.code() != 304) return false;

    if (cacheEntity == null) {
        final Response<T> error = Response.error(true, call, response, CacheException.NON_AND_304(request.getCacheKey()));
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mCallback.onError(error);
                mCallback.onFinish();
            }
        });
    } else {
        final Response<T> success = Response.success(true, cacheEntity.getData(), call, response);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mCallback.onCacheSuccess(success);
                mCallback.onFinish();
            }
        });
    }
    return true;
}
 
Example 7
public void getAccounteData(String account) {

        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("name", account);
        HttpUtils.postRequest(BaseUrl.HTTP_eos_get_account, mContext, hashMap, new JsonCallback<ResponseBean<AccountDetailsBean>>() {
            @Override
            public void onSuccess(Response<ResponseBean<AccountDetailsBean>> response) {
                if (response.body().code == 0) {
                    view.getAccountDetailsDataHttp(response.body().data);
                } else {
                    view.getDataHttpFail(response.body().message);
                }
            }
        });


    }
 
Example 8
public void getAccountInfoData(String accountname) {
    HashMap<String, String> hashMap = new HashMap<String, String>();
    hashMap.put("name", accountname);
    HttpUtils.postRequest(BaseUrl.HTTP_get_chain_account_info, mContext, hashMap, new JsonCallback<ResponseBean<BlockChainAccountInfoBean.DataBean>>() {
        @Override
        public void onSuccess(Response<ResponseBean<BlockChainAccountInfoBean.DataBean>> response) {
            if (response.body().code == 0) {
                view.getBlockchainAccountInfoDataHttp(response.body().data);
            } else {
                view.getDataHttpFail(response.body().message);
            }
        }
    });


}
 
Example 9
public void postEosAccountData(String eosAccountName ,String owner_key, String active_key) {
    HashMap<String, String> hashMap = new HashMap<String, String>();
    hashMap.put("uid", MyApplication.getInstance().getUserBean().getWallet_uid());
    hashMap.put("eosAccountName", eosAccountName);
    hashMap.put("ownerKey", owner_key);
    hashMap.put("activeKey", active_key);
    HttpUtils.postRequest(BaseUrl.HTTP_add_new_eos, mContext, hashMap, new JsonCallback<ResponseBean<String>>() {
        @Override
        public void onSuccess(Response<ResponseBean<String>> response) {
            if (response.body().code == 0) {
                view.postEosAccountDataHttp();
            } else {
                view.getDataHttpFail(response.body().message);
            }
        }
    });
}
 
Example 10
public void getRequreKey(GetRequiredKeys getRequiredKeys) {

        HttpUtils.postRequest(BaseUrl.HTTP_get_required_keys, this, mGson.toJson(getRequiredKeys), new JsonCallback<ResponseBean>() {
            @Override
            public void onSuccess(Response<ResponseBean> response) {
                if (response.body().code == 0) {
                    RequreKeyResult requreKeyResult = (RequreKeyResult) JsonUtil.parseStringToBean(mGson.toJson(response.body().data), RequreKeyResult.class);
                    EosPrivateKey eosPrivateKey = new EosPrivateKey(PublicAndPrivateKeyUtils.getPrivateKey(requreKeyResult.getRequired_keys().get(0), userpassword));
                    txnBeforeSign.sign(eosPrivateKey, new TypeChainId(mChainInfoBean.getChain_id()));
                    pushTransactionRetJson(new PackedTransaction(txnBeforeSign));
                } else {
                    if (ShowDialog.dialog != null) {
                        ShowDialog.dissmiss();
                    }
                    ToastUtils.showLongToast(response.body().message);
                    mCallback.erroMsg(response.body().data.toString());
                }
            }
        });

    }
 
Example 11
Source Project: okhttp-OkGo   Source File: RequestFailedCachePolicy.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void onError(final Response<T> error) {

    if (cacheEntity != null) {
        final Response<T> cacheSuccess = Response.success(true, cacheEntity.getData(), error.getRawCall(), error.getRawResponse());
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mCallback.onCacheSuccess(cacheSuccess);
                mCallback.onFinish();
            }
        });
    } else {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mCallback.onError(error);
                mCallback.onFinish();
            }
        });
    }
}
 
Example 12
public void setMianAccountData(String eosAccountName) {

        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("uid", MyApplication.getInstance().getUserBean().getWallet_uid());
        hashMap.put("eosAccountName", eosAccountName);
        HttpUtils.postRequest(BaseUrl.HTTP_set_mian_account, mContext, hashMap, new JsonCallback<ResponseBean<String>>() {
            @Override
            public void onSuccess(Response<ResponseBean<String>> response) {
                if (response.body().code == 0) {
                    view.setMainAccountHttp();
                } else {
                    view.getDataHttpFail(response.body().message);
                }
            }
        });
    }
 
Example 13
Source Project: okhttp-OkGo   Source File: DefaultCachePolicy.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void requestAsync(CacheEntity<T> cacheEntity, Callback<T> callback) {
    mCallback = callback;
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            mCallback.onStart(request);

            try {
                prepareRawCall();
            } catch (Throwable throwable) {
                Response<T> error = Response.error(false, rawCall, null, throwable);
                mCallback.onError(error);
                return;
            }
            requestNetworkAsync();
        }
    });
}
 
Example 14
@Override
protected void initData() {

    HttpUtils.getRequets(BaseUrl.HTTP_get_app_info, AppUpdateActivity.this, new HashMap<String, String>(), new JsonCallback<ResponseBean<UpdateAppBean.DataBean>>() {
        @Override
        public void onSuccess(Response<ResponseBean<UpdateAppBean.DataBean>> response) {
            if (response.body().code == 0) {
                if (Integer.parseInt(response.body().data.getVersionCode()) > AppUtil.getAppVersionCode(AppUpdateActivity.this)) {
                    mIsNewVersion.setText(R.string.find_new_app);
                    mIsNewVersion.setTextColor(getResources().getColor(R.color.red_packet_color));
                } else {
                    mIsNewVersion.setText(R.string.no_new_app);
                    mIsNewVersion.setTextColor(getResources().getColor(R.color.txt_color));
                }
            }
        }
    });
}
 
Example 15
public void bindQQ(String openid, String name, String avatar) {
    HashMap<String, String> hashMap = new HashMap<String, String>();
    hashMap.put("uid", MyApplication.getInstance().getUserBean().getWallet_uid());
    hashMap.put("openid", openid);
    hashMap.put("name", name);
    hashMap.put("avatar", avatar);
    HttpUtils.postRequest(BaseUrl.HTTP_bindQQ, mContext, hashMap, new JsonCallback<ResponseBean<String>>() {
        @Override
        public void onSuccess(Response<ResponseBean<String>> response) {
            if (response.body().code == 0) {
                view.bindOtherLoginDataHttp();
            } else {
                view.getDataHttpFail(response.body().message);
            }
        }
    });
}
 
Example 16
Source Project: okhttp-OkGo   Source File: TestActivity.java    License: Apache License 2.0 6 votes vote down vote up
@OnClick(R.id.btn2)
public void btn2(View view) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Call<JSONObject> adapt = OkGo.<JSONObject>get(Urls.URL_JSONOBJECT).adapt();
                Response<JSONObject> response = adapt.execute();
                System.out.println("body " + response.body());
                Throwable exception = response.getException();
                if (exception != null) exception.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }).start();
}
 
Example 17
Source Project: BaseProject   Source File: NoCachePolicy.java    License: Apache License 2.0 6 votes vote down vote up
@Override
    public void onError(final Response<T> error) {
        if (mCallback != null) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    callbackOnError(error);
                    callbackOnFinish();
//                    if (mCallback != null) {
//                        mCallback.onError(error);
//                        mCallback.onFinish();
//                    }
                }
            });
        }
    }
 
Example 18
Source Project: okhttp-OkGo   Source File: RequestFailedCachePolicy.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void requestAsync(CacheEntity<T> cacheEntity, Callback<T> callback) {
    mCallback = callback;
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            mCallback.onStart(request);

            try {
                prepareRawCall();
            } catch (Throwable throwable) {
                Response<T> error = Response.error(false, rawCall, null, throwable);
                mCallback.onError(error);
                return;
            }
            requestNetworkAsync();
        }
    });
}
 
Example 19
Source Project: okhttp-OkGo   Source File: NoCachePolicy.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void requestAsync(CacheEntity<T> cacheEntity, Callback<T> callback) {
    mCallback = callback;
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            mCallback.onStart(request);

            try {
                prepareRawCall();
            } catch (Throwable throwable) {
                Response<T> error = Response.error(false, rawCall, null, throwable);
                mCallback.onError(error);
                return;
            }
            requestNetworkAsync();
        }
    });
}
 
Example 20
Source Project: BaseProject   Source File: NoCachePolicy.java    License: Apache License 2.0 6 votes vote down vote up
@Override
    public void requestAsync(CacheEntity<T> cacheEntity, Callback<T> callback) {
        mCallback = callback;
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                callbackOnStart(request);
//                if (mCallback != null) {
//                    mCallback.onStart(request);
//                }
                try {
                    prepareRawCall();
                } catch (Throwable throwable) {
                    Response<T> error = Response.error(false, rawCall, null, throwable);
                    callbackOnError(error);
//                    mCallback.onError(error);
                    return;
                }
                requestNetworkAsync();
            }
        });
    }
 
Example 21
Source Project: okhttp-OkGo   Source File: FirstCacheRequestPolicy.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void requestAsync(final CacheEntity<T> cacheEntity, Callback<T> callback) {
    mCallback = callback;
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            mCallback.onStart(request);

            try {
                prepareRawCall();
            } catch (Throwable throwable) {
                Response<T> error = Response.error(false, rawCall, null, throwable);
                mCallback.onError(error);
                return;
            }
            if (cacheEntity != null) {
                Response<T> success = Response.success(true, cacheEntity.getData(), rawCall, null);
                mCallback.onCacheSuccess(success);
            }
            requestNetworkAsync();
        }
    });
}
 
Example 22
public void getRequreKey(GetRequiredKeys getRequiredKeys) {
    HttpUtils.postRequest(BaseUrl.HTTP_get_required_keys, this, mGson.toJson(getRequiredKeys), new JsonCallback<ResponseBean>() {
        @Override
        public void onSuccess(Response<ResponseBean> response) {
            if (response.body().code == 0) {
                RequreKeyResult requreKeyResult = (RequreKeyResult) JsonUtil.parseStringToBean(mGson.toJson(response.body().data), RequreKeyResult.class);
                EosPrivateKey eosPrivateKey = new EosPrivateKey(PublicAndPrivateKeyUtils.getPrivateKey(requreKeyResult.getRequired_keys().get(0), userpassword));
                txnBeforeSign.sign(eosPrivateKey, new TypeChainId(mChainInfoBean.getChain_id()));
                pushTransactionRetJson(new PackedTransaction(txnBeforeSign));
            } else {
                if (ShowDialog.dialog != null) {
                    ShowDialog.dissmiss();
                }
                ToastUtils.showLongToast(response.body().message);
            }
        }
    });
}
 
Example 23
public void getChainInfo() {
    HttpUtils.getRequets(BaseUrl.HTTP_get_chain_info, this, new HashMap<String, String>(), new JsonCallback<ResponseBean>() {
        @Override
        public void onSuccess(Response<ResponseBean> response) {
            if (response.body().code == 0) {
                mChainInfoBean = (EosChainInfo) JsonUtil.parseStringToBean(mGson.toJson(response.body().data), EosChainInfo.class);
                getabi_json_to_bin();
            } else {
                if (ShowDialog.dialog != null) {
                    ShowDialog.dissmiss();
                }
                ToastUtils.showLongToast(response.body().message);
                mCallback.getResult(new Gson().toJson(response.body().data));
            }
        }
    });
}
 
Example 24
public void getabi_json_to_bin() {
    JsonToBinRequest jsonToBinRequest = new JsonToBinRequest(contract, action, message.replaceAll("\\r|\\n", ""));
    HttpUtils.postRequest(BaseUrl.HTTP_get_abi_json_to_bin, this, mGson.toJson(jsonToBinRequest), new JsonCallback<ResponseBean>() {
        @Override
        public void onSuccess(Response<ResponseBean> response) {
            if (response.body().code == 0) {
                mJsonToBeanResultBean = (JsonToBeanResultBean) JsonUtil.parseStringToBean(mGson.toJson(response.body().data), JsonToBeanResultBean.class);
                txnBeforeSign = createTransaction(contract, action, mJsonToBeanResultBean.getBinargs(), permissions, mChainInfoBean);
                //扫描钱包列出所有可用账号的公钥
                List<String> pubKey = PublicAndPrivateKeyUtils.getActivePublicKey();

                getRequreKey(new GetRequiredKeys(txnBeforeSign, pubKey));
            } else {
                if (ShowDialog.dialog != null) {
                    ShowDialog.dissmiss();
                }
                ToastUtils.showLongToast(response.body().message);
                mCallback.getResult(new Gson().toJson(response.body().data));
            }
        }
    });
}
 
Example 25
public void pushTransactionRetJson(PackedTransaction body) {
    HttpUtils.postRequest(BaseUrl.HTTP_push_transaction, this, mGson.toJson(body), new JsonCallback<ResponseBean>() {
        @Override
        public void onSuccess(final Response<ResponseBean> response) {
            if (ShowDialog.dialog != null) {
                ShowDialog.dissmiss();
            }
            if (response.body().code == 0) {
                PushSuccessBean.DataBeanX dataBeanX = (PushSuccessBean.DataBeanX) JsonUtil.parseStringToBean(mGson.toJson(response.body().data), PushSuccessBean.DataBeanX.class);
                mCallback.getResult("transaction_id=" + dataBeanX.getTransaction_id());
            } else {
                ToastUtils.showLongToast(response.body().message);
                mCallback.getResult("ERROR:" + response.body().data);
            }
        }
    });
}
 
Example 26
public void getRequreKey(GetRequiredKeys getRequiredKeys) {

        HttpUtils.postRequest(BaseUrl.HTTP_get_required_keys, this, mGson.toJson(getRequiredKeys), new JsonCallback<ResponseBean>() {
            @Override
            public void onSuccess(Response<ResponseBean> response) {
                if (response.body().code == 0) {
                    RequreKeyResult requreKeyResult = (RequreKeyResult) JsonUtil.parseStringToBean(mGson.toJson(response.body().data), RequreKeyResult.class);
                    EosPrivateKey eosPrivateKey = new EosPrivateKey(PublicAndPrivateKeyUtils.getPrivateKey(requreKeyResult.getRequired_keys().get(0), userpassword));
                    txnBeforeSign.sign(eosPrivateKey, new TypeChainId(mChainInfoBean.getChain_id()));
                    pushTransactionRetJson(new PackedTransaction(txnBeforeSign));
                } else {
                    if (ShowDialog.dialog != null) {
                        ShowDialog.dissmiss();
                    }
                    ToastUtils.showLongToast(response.body().message);
                }
            }
        });

    }
 
Example 27
Source Project: DevUtils   Source File: OkGoCallback.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 缓存成功的回调,UI 线程
 */
@Override
public void onCacheSuccess(Response<String> response) {
    super.onCacheSuccess(response);

    // response.getRawCall().request().url()

    DevLogger.dTag(TAG, "缓存成功的回调: " + url);
}
 
Example 28
public void getRedPacketDetailsData(String redpacket_id) {
    HashMap<String, String> hashMap = new HashMap<String, String>();
    hashMap.put("id", redpacket_id);
    HttpUtils.postRequest(BaseUrl.getHTTP_get_red_packet_details_history, mContext, hashMap, new JsonCallback<ResponseBean<RedPacketDetailsBean.DataBean>>() {
        @Override
        public void onSuccess(Response<ResponseBean<RedPacketDetailsBean.DataBean>> response) {
            if (response.body().code == 0) {
                view.getRedPacketDetailsDataHttp(response.body().data);
            } else {
                view.getDataHttpFail(response.body().message);
            }
        }
    });
}
 
Example 29
Source Project: okhttp-OkGo   Source File: BaseCachePolicy.java    License: Apache License 2.0 5 votes vote down vote up
protected Response<T> requestNetworkSync() {
    try {
        okhttp3.Response response = rawCall.execute();
        int responseCode = response.code();

        //network error
        if (responseCode == 404 || responseCode >= 500) {
            return Response.error(false, rawCall, response, HttpException.NET_ERROR());
        }

        T body = request.getConverter().convertResponse(response);
        //save cache when request is successful
        saveCache(response.headers(), body);
        return Response.success(false, body, rawCall, response);
    } catch (Throwable throwable) {
        if (throwable instanceof SocketTimeoutException && currentRetryCount < request.getRetryCount()) {
            currentRetryCount++;
            rawCall = request.getRawCall();
            if (canceled) {
                rawCall.cancel();
            } else {
                requestNetworkSync();
            }
        }
        return Response.error(false, rawCall, null, throwable);
    }
}
 
Example 30
Source Project: okhttp-OkGo   Source File: FirstCacheRequestPolicy.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void onError(final Response<T> error) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            mCallback.onError(error);
            mCallback.onFinish();
        }
    });
}