1. 我正在从服务器获取大量数据并在Android登录时插入本地数据库。
2. 为了同步目的,我使用了Retrofit库,并且同步和插入工作正常。
我的问题是:在使用Retrofit从服务器同步大量数据时,圆形进度对话框会冻结。
请帮忙解决这个问题。
编辑:1
在异步任务中调用Retrofit方法,但圆形ProgressDialog仍然会冻结。
//调用异步任务
Asynctask_Getdata task=new Asynctask_Getdata(TokenType_StringValue,Access_Token_StringValue, ID_StringValue);
task.execute();
//异步任务方法
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();
}
}
});
}
execute
方法的?你能展示一下代码的部分吗? - Opiatefuchs