Android 数据加载两次。。。意味着AsyncTask onPostExecute加载相同的数据两次?
数据加载两次。。。意味着AsyncTask onPostExecute加载相同的数据两次? myAsyncTask onPostExecute运行两次,加载相同的数据。。。 我正在运行异步任务以从服务器加载结果。在gridview中显示数据,但数据显示两次..我不明白为什么AsyncTask会输出两次数据。 请帮帮我 这是我的密码:Android 数据加载两次。。。意味着AsyncTask onPostExecute加载相同的数据两次?,android,android-asynctask,Android,Android Asynctask,数据加载两次。。。意味着AsyncTask onPostExecute加载相同的数据两次? myAsyncTask onPostExecute运行两次,加载相同的数据。。。 我正在运行异步任务以从服务器加载结果。在gridview中显示数据,但数据显示两次..我不明白为什么AsyncTask会输出两次数据。 请帮帮我 这是我的密码: public class InfoAccountsascyn extends AsyncTask <String, String, String>{
public class InfoAccountsascyn extends AsyncTask <String, String, String>{
String result;
String s;
private Activity activity;
private String url;
private String user_id;
private ProgressDialog pDialog;
JSONObject json;
JSONParser jsonParser = new JSONParser();
private static final String TAG_SUCCESS = "Success";
int Success;
public InfoAccountsascyn(Activity activity, String url,String user_id) {
super();
this.activity = activity;
this.url = url;
this.user_id=user_id;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(activity);
pDialog.setMessage("Loading Items...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.setCanceledOnTouchOutside(false);
pDialog.show();
}
protected String doInBackground(String... args) {
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("customer_id", user_id));
//if(Last_id!=null){
// params.add(new BasicNameValuePair("last_product_id", Last_id));
// }
json = jsonParser.makeHttpRequest(url, "POST", params);
Success = json.getInt(TAG_SUCCESS);
} catch (JSONException e) {
e.printStackTrace();
}
if (Success == 1) {
result = json.toString();
}else{
try {
result = json.getString("CartData");
//s=result;
} catch (JSONException e) {
e.printStackTrace();
}
}
return result;
}
protected void onPostExecute(String result) {
((DetaildAddress) activity).parseJsonResponse(result);
pDialog.dismiss();
}
}
公共类InfoAccountsascyn扩展异步任务{
字符串结果;
字符串s;
私人活动;
私有字符串url;
私有字符串用户标识;
私人对话;
JSONObject json;
JSONParser JSONParser=新的JSONParser();
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
成功;
public InfoAccountsascyn(活动活动、字符串url、字符串用户id){
超级();
这个。活动=活动;
this.url=url;
this.user\u id=user\u id;
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新建进度对话框(活动);
setMessage(“正在加载项…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.setCanceledOnTouchOut(假);
pDialog.show();
}
受保护的字符串doInBackground(字符串…args){
试一试{
List params=new ArrayList();
参数add(新的BasicNameValuePair(“客户id”,用户id));
//如果(最后一个\u id!=null){
//参数add(新的BasicNameValuePair(“last_product_id”,last_id));
// }
json=jsonParser.makeHttpRequest(url,“POST”,参数);
Success=json.getInt(TAG_Success);
}捕获(JSONException e){
e、 printStackTrace();
}
如果(成功==1){
result=json.toString();
}否则{
试一试{
result=json.getString(“CartData”);
//s=结果;
}捕获(JSONException e){
e、 printStackTrace();
}
}
返回结果;
}
受保护的void onPostExecute(字符串结果){
((DetaildAddress)活动);
pDialog.disclose();
}
}
这是parseJsonResponse
public void parseJsonResponse(String result) {
String res = result;
if (res !=null) {
try {
JSONObject jObjects = new JSONObject(result);
JAS = jObjects.getJSONArray("Data");
for (int i = 0; i < JAS.length(); i++) {
JSONObject c = JAS.getJSONObject(i);
DetailedAddressInfo productinfo = new DetailedAddressInfo();
productinfo.setFirstname(c.optString("firstname"));
productinfo.setLastname(c.optString("lastname"));
productinfo.setStreet(c.optString("street"));
productinfo.setCity(c.optString("city"));
productinfo.setRegion(c.optString("region"));
productinfo.setPostcode(c.optString("postcode"));
productinfo.setTelephone(c.optString("telephone"));
productinfo.setFax(c.optString("fax"));
productinfo.setAddId(c.optString("customer_address_id"));
productinfo.setBilling(c.optString("is_default_billing"));
productinfo.setShipping(c.optString("is_default_shipping"));
products.add(productinfo);
if (i == JAS.length() - 1) {
Last_id = c.optString("product_id");
}
}
adapter = new DetailedAddressAdapter(DetaildAddress.this, R.layout.deshbord_address, products);
gridView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
else {
new AlertDialog.Builder(DetaildAddress.this)
.setTitle("No Address Available")
.setMessage("You Want to Add New Address")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent=new Intent(DetaildAddress.this,NewAddress.class);
String ok = "0";
intent.putExtra("ok", ok);
startActivity(intent);
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
} if (pDialog != null) {
pDialog.dismiss();
}
}
public void parseJsonResponse(字符串结果){
字符串res=结果;
如果(res!=null){
试一试{
JSONObject jObjects=新JSONObject(结果);
JAS=jObjects.getJSONArray(“数据”);
for(int i=0;i
这是我调用AsyncTask的OnCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detaild_address);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
stub = (ViewStub) findViewById(R.id.layout_stub);
stub.setLayoutResource(R.layout.content_detaild_address);
View inflated = stub.inflate();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
String check = "0";
String chEck;
DBHandler db = new DBHandler(DetaildAddress.this);
List<DBconnector> Contacts = db.getAllContacts();
for (DBconnector cn : Contacts) {
chEck = cn.getActive();
if(chEck != null){
if (chEck.equals("1")){
check = chEck;
}
}
}
if (check.equals("1")) {
List<DBconnector> contacts = db.getActiveData();
for (DBconnector c : contacts) {
Customer_id = c.getCustomer_id();
//new CheckAddress().execute();
boolean connected = false;
ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
if(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ||
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {
//we are connected to a network
new InfoAccountsascyn(DetaildAddress.this,url_address,Customer_id).execute();
}
else
{
Toast.makeText(DetaildAddress.this,"Internet Connection is not Available",Toast.LENGTH_LONG).show();
Intent intent=new Intent(DetaildAddress.this,HomeActivity.class);
startActivity(intent);
connected = false;
}
}
}
else{
Toast.makeText(DetaildAddress.this, "Please login First", Toast.LENGTH_LONG).show();
Intent intent = new Intent(DetaildAddress.this, UserLogin.class);
startActivity(intent);
}
gridView = (GridView) findViewById(R.id.gridView1);
myLastVisiblePos = gridView.getFirstVisiblePosition();
products = new ArrayList<DetailedAddressInfo>();
//gridView.setAdapter(new DetailedAddressAdapter(DetaildAddress.this, R.layout.deshbord_address, products));
TextView newaddress=(TextView)findViewById(R.id.newaddress);
newaddress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(DetaildAddress.this,NewAddress.class);
String ok = "0";
intent.putExtra("ok", ok);
startActivity(intent);
}
});
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u detaild\u地址);
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
设置支持操作栏(工具栏);
存根=(视图存根)findViewById(R.id.layout\u存根);
stub.setLayoutResource(R.layout.content\u detaild\u地址);
视图充气=存根充气();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
字符串检查=“0”;
字符串检查;
DBHandler db=新的DBHandler(DetaildAddress.this);
List Contacts=db.getAllContacts();
用于(DBCN连接器:触点){
chEck=cn.getActive();
如果(检查!=null){
如果(检查等于(“1”)){
检查=检查;
}
}
}
如果(检查等于(“1”)){
List contacts=db.getActiveData();
用于(DBC连接器:触点){
Customer_id=c.getCustomer_id();
//新建CheckAddress().execute();
布尔连接
if(products.size()>0)
products.clear();