Kum*_*mar 9 android progressdialog progress-bar retrofit
1. 在Android中登录时,我从服务器获取大量数据并插入本地数据库.
2.用于同步目的我使用了改造库,同步和插入工作正常.
我的问题:从服务器同步数据同时使用改造,循环进度对话框获得冻结.
帮助我解决此问题.
编辑:1
在Async任务中调用Retrofit方法,Still Circle ProgressDialog获取冻结
//调用异步任务
Asynctask_Getdata task=new Asynctask_Getdata(TokenType_StringValue,Access_Token_StringValue, ID_StringValue);
task.execute();
Run Code Online (Sandbox Code Playgroud)
// AsyncTask方法
public class Asynctask_Getdata extends AsyncTask<String,Void,Void>
{
String TokenType_StringValueitem;
String Access_Token_StringValueitem;
String ID_StringValueitem;
public Asynctask_Getdata(String tokenType_StringValueitem, String access_Token_StringValueitem, String ID_StringValueitem) {
TokenType_StringValueitem = tokenType_StringValueitem;
Access_Token_StringValueitem = access_Token_StringValueitem;
ID_StringValueitem = ID_StringValueitem;
}
@Override
protected void onPreExecute()
{
super.onPreExecute();
if (!pDialog.isShowing())
{
pDialog.setIndeterminate(true);
pDialog.setCanceledOnTouchOutside(false);
pDialog.setMessage("Please Wait Getting Data...");
pDialog.show();
}
}
@Override
protected void onPostExecute(Void aVoid)
{
super.onPostExecute(aVoid);
}
@Override
protected Void doInBackground(String... params)
{
getLoginDataCall(TokenType_StringValueitem, Access_Token_StringValueitem, ID_StringValueitem );
return null;
}
}
private void getLoginDataCall(String TokenType_String, String Access_Token_StringValue, String RouteID_String) {
String Tokenadd = TokenType_String + " Access_Token_StringValue;
sisClient.getApi().getAllData(Tokenadd, RouteID_String,
new Callback<CommonResponse>() {
@Override
public void success(CommonResponse commonResponse, Response response) {
Timber.d("sendOtpAPICall%s", commonResponse.getStatusCode());
switch (commonResponse.getStatusCode()) {
case 200:
try {
JSONArray jsonarray = null;
try {
jsonarray = new JSONArray(commonResponse.getRouteMaster());
if (!commonResponse.getRouteMaster().equals("[]"))
{
for (int i = 0; i < jsonarray.length(); i++)
{
JSONObject jsonobject = jsonarray.getJSONObject(i);
RouteId_StringValue = jsonobject.getString("RouteId");
Asynxfor_Route_Master_insert(RouteId_StringValue);
}
} else {
System.out.println("ROute Master Is NULL ::" + commonResponse.getStatusMessage() + "\n");
}
} catch (Exception e) {
e.printStackTrace();
;
}
break;
case 404:
pDialog.dismiss();
Toast.makeText(LoginPage.this, R.string.wrong_Username_or_password, Toast.LENGTH_LONG).show();
break;
case 500:
pDialog.dismiss();
Toast.makeText(LoginPage.this, R.string.something_wrong, Toast.LENGTH_LONG).show();
break;
}
}
@Override
public void failure(RetrofitError error) {
try {
if (error != null) {
pDialog.dismiss();
Timber.i("sendOtpAPICall error %s", error.getResponse().getStatus());
String json = new String(((TypedByteArray) error.getResponse().getBody()).getBytes());
Timber.i("failure error %s", json.toString());
JSONObject json1 = new JSONObject(json.toString());
String json1string = json1.getString("StatusMessage");
switch (error.getResponse().getStatus()) {
case 404:
Toast.makeText(getApplicationContext(), R.string.wrong_Username_or_password, Toast.LENGTH_LONG).show();
break;
case 500:
Toast.makeText(LoginPage.this, R.string.something_wrong, Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(LoginPage.this, json1string, Toast.LENGTH_LONG).show();
break;
}
} else {
Timber.i("failure error %s", "Recieving error null rom server");
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(LoginPage.this, R.string.something_wrong, Toast.LENGTH_LONG).show();
}
}
});
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,没有充分的理由进行同步 Retrofit 调用,您应该使用Retrofit 开箱即用提供的异步调用。这非常简单,您可以使用回调来管理 ProgressBar 状态。
IMO 你根本不应该使用 AsyncTask。如果你想探索这个,请查看这个、这个和这个。
从使用 AsyncTask 的 Retrofit v1 同步调用到 Retrofit 2 异步调用的示例代码转换,在 api 类中管理并使用 Otto 事件总线进行消息传递:
public class ApiService {
private static ApiService INSTANCE;
// thwart instantiation by protecting
protected ApiService() {
// nothing
}
public static ApiService getInstance() {
if (INSTANCE == null) {
INSTANCE = new ApiService();
}
return INSTANCE;
}
// -----------------------------------------------------------------------------------
private static final String BASE_URL = "http://api.service.com/";
private interface ApiServiceInterface {
@GET("your/endpoint")
Call<CommonResponse> postLogin(@Query("token_add") String tokenAdd, @Query("route_id") String RouteId);
}
private ApiServiceInterface apiServiceInterface = null;
private ApiServiceInterface getInterface() {
if (apiServiceInterface == null) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.build();
Retrofit retrofit = new Retrofit.Builder()
.client(client)
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
apiServiceInterface = retrofit.create(ApiServiceInterface.class);
}
return apiServiceInterface;
}
public void doGetData(Dialog pDialog, String TokenType_String, String Access_Token_StringValue, String RouteID_String) {
if (!pDialog.isShowing())
{
pDialog.setIndeterminate(true);
pDialog.setCanceledOnTouchOutside(false);
pDialog.setMessage("Please Wait Getting Data...");
pDialog.show();
}
String Tokenadd = TokenType_String + Access_Token_StringValue;
getInterface().postLogin(Tokenadd, RouteID_String)
.enqueue(new Callback<SocketCtrlResponse>() {
@Override
public void onResponse(Call<CommonResponse> call, Response<CommonResponse> response) {
Timber.d("sendOtpAPICall%s", commonResponse.getStatusCode());
switch (commonResponse.getStatusCode()) {
case 200:
JSONArray jsonarray = null;
try {
jsonarray = new JSONArray(commonResponse.getRouteMaster());
if (!commonResponse.getRouteMaster().equals("[]")) {
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
RouteId_StringValue = jsonobject.getString("RouteId");
//Asynxfor_Route_Master_insert(RouteId_StringValue);
EventBus.getDefault().post(new GetDataResponseEvent(RouteId_StringValue));
}
} else {
System.out.println("ROute Master Is NULL ::" + commonResponse.getStatusMessage() + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
break;
case 404:
pDialog.dismiss();
Toast.makeText(LoginPage.this, R.string.wrong_Username_or_password, Toast.LENGTH_LONG).show();
EventBus.getDefault().post(new GetDataResponseEvent(""));
break;
case 500:
pDialog.dismiss();
Toast.makeText(LoginPage.this, R.string.something_wrong, Toast.LENGTH_LONG).show();
EventBus.getDefault().post(new GetDataResponseEvent(""));
break;
}
}
@Override
public void onFailure(Call<CommonResponse> call, Throwable t) {
t.printStackTrace();
pDialog.dismiss();
Toast.makeText(LoginPage.this, R.string.something_wrong, Toast.LENGTH_LONG).show();
EventBus.getDefault().post(new GetDataResponseEvent(""));
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
您需要一个事件类来包装您希望从 API 返回的数据,例如:
public class GetDataResponseEvent {
private final String data;
public GetDataResponseEvent(String data) {
this.data = data;
}
public String getData() {
return data;
}
}
Run Code Online (Sandbox Code Playgroud)
作为从以下位置调用服务的示例Activity:
public class YourActivity extends Activity {
Dialog pDialog;
// ...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// init pDialog, content view, etc.
EventBus.getDefault().register(this);
// ...
}
// ...
public void getDataFromApi() {
ApiService.getInstance().doGetData(pDialog, TokenType_StringValue,Access_Token_StringValue, ID_StringValue);
}
// ...
public void onEvent(GetDataResponseEvent event) {
String data = event.getData();
Asynxfor_Route_Master_insert(data);
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
再次注意,您必须升级到Retrofit 2并使用Otto。我猜到了这个实现,所以它可能无法按原样工作,但应该是一个很好的草图来感受它。
| 归档时间: |
|
| 查看次数: |
1088 次 |
| 最近记录: |