2013-01-17 182 views
2

我正在开发一个简单的应用程序,它将伪随机字符串发送到服务器数据库。在这里,我使用AsyncTask将字符串发送到前台的服务器和ProgressDialog。问题是进度对话框没有停止,我不在服务器上输入任何字符串。也许在发送srcetring的代码中存在一些问题。我是Android新手,并从互联网上的可用来源进行学习。这是我使用的代码。ProgressDialog不停止与AsyncTask

public class MainActivity extends Activity { 

    Button btn; 
    TextView tv; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     tv = (TextView)findViewById(R.id.textView2); 
     btn = (Button)findViewById(R.id.button1); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

    public void getThis(View v) { 
     String str = "35" + 
      Build.BOARD.length()%10+ Build.BRAND.length()%10 + 
      Build.CPU_ABI.length()%10 + Build.DEVICE.length()%10 + 
      Build.DISPLAY.length()%10 + Build.HOST.length()%10 + 
      Build.ID.length()%10 + Build.MANUFACTURER.length()%10 + 
      Build.MODEL.length()%10 + Build.PRODUCT.length()%10 + 
      Build.TAGS.length()%10 + Build.TYPE.length()%10 + 
      Build.USER.length()%10 ; 
     tv.setText(str); 
     UploadUniqueID uni=new UploadUniqueID(this,str); 
     uni.execute(str); 
    } 

    class UploadUniqueID extends AsyncTask<String, Integer, String> { 

     Context context; 
     MainActivity ma; 
     ProgressDialog dialog; 
     String id; 
     public UploadUniqueID(MainActivity activity,String str) { 
      ma = activity; 
      context = activity; 
      dialog = new ProgressDialog(context); 
      id = str; 
     } 

     protected void onPreExecute() { 
      this.dialog.setMessage("Progress start"); 
      this.dialog.setCancelable(true); 
      this.dialog.show(); 
     } 

     @Override 
     protected String doInBackground(String... params) { 
      // perform long running operation operation 
      String id=params[0]; 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("http://10.0.2.2/UniqueIdApp/myPHP.php"); 

      try { 
       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); 
       nameValuePairs.add(new BasicNameValuePair("android",id));  
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       httpclient.execute(httppost);   
      } catch (Exception e) { 
       Log.i("HTTP Failed", e.toString()); 
      }    

      return null; 
     } 

     protected void onProgressUpdate(Integer...integers) { 

     } 

     protected void onPostExecute(String...strings) { 
      tv.setText("Sent"); 
      if (dialog.isShowing()) { 
       dialog.dismiss(); 
      } 
     } 
    } 
} 
+0

如果您收到任何错误的logcat的帖子在这里 –

+0

都能跟得上..我没有得到任何错误.. – apatel

回答

0

您还可以创建并通过这样做在你的UploadUniqueID的AsyncTask类初始化的对话框:

private final ProgressDialog dialog = new ProgressDialog(MainActivity.this); 

,只是关闭它在你的onPostExecute(...)方法,你是正在做。另外,将@Override注释添加到您的onPreExecute()方法中。

+0

嘿它的工作..但它采取几乎一分钟完成该过程。这可能是什么原因呢? – apatel

+0

@aakash它正在执行一个与网络相关的任务,所以时间根据你试图通过Web服务拉取多少数据而变化。然而,这是正常的,你可以做的是尝试和优化PHP端的代码,看看是否加快速度。 –

+0

Thanx兄弟..它帮了很多。 – apatel

0

我建议你来初始化protected void onCreate(Bundle savedInstanceState)方法你ProgressDialog dialog对象,并在onPostExecute(String...strings)方法关闭该进度条。可能是这会帮助你欢呼:)。

编辑

public class MainActivity extends Activity { 
ProgressDialog dialog; 
Button btn; 
TextView tv; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    tv = (TextView)findViewById(R.id.textView2); 
    btn = (Button)findViewById(R.id.button1); 
    dialog = new ProgressDialog(MainActivity.this); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

public void getThis(View v) { 
    String str = "35" + 
     Build.BOARD.length()%10+ Build.BRAND.length()%10 + 
     Build.CPU_ABI.length()%10 + Build.DEVICE.length()%10 + 
     Build.DISPLAY.length()%10 + Build.HOST.length()%10 + 
     Build.ID.length()%10 + Build.MANUFACTURER.length()%10 + 
     Build.MODEL.length()%10 + Build.PRODUCT.length()%10 + 
     Build.TAGS.length()%10 + Build.TYPE.length()%10 + 
     Build.USER.length()%10 ; 
    tv.setText(str); 
    UploadUniqueID uni=new UploadUniqueID(this,str); 
    uni.execute(str); 
} 

class UploadUniqueID extends AsyncTask<String, Integer, String> { 

    Context context; 
    MainActivity ma; 
    ProgressDialog dialog; 
    String id; 
    /*public UploadUniqueID(MainActivity activity,String str) { 
     ma = activity; 
     context = activity; 
     dialog = new ProgressDialog(context); 
     id = str; 
    }*/ 

    protected void onPreExecute() { 
     dialog.setMessage("Progress start"); 
     dialog.setCancelable(true); 
     dialog.show(); 
    } 

    @Override 
    protected String doInBackground(String... params) { 
     // perform long running operation operation 
     String id=params[0]; 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http://10.0.2.2/UniqueIdApp/myPHP.php"); 

     try { 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); 
      nameValuePairs.add(new BasicNameValuePair("android",id));  
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      httpclient.execute(httppost);   
     } catch (Exception e) { 
      Log.i("HTTP Failed", e.toString()); 
     }    

     return null; 
    } 

    protected void onProgressUpdate(Integer...integers) { 

    } 

    protected void onPostExecute(String...strings) { 
     tv.setText("Sent"); 
     try{ 
    if(dialog!=null && dialog.isShowing()){ 
     dialog.setMessage("DownLoading finished"); 
     dialog.dismiss(); 
    } 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } 
    } 
    } 
} 
+0

我试了一下。但不工作 – apatel

+0

有什么你试过了吗???你能显示你尝试过的代码片段吗? – BBdev

+0

我编辑了你的代码现在看看它的工作与否? – BBdev

0

试试下面的代码:

public class MainActivity extends Activity { 
    Button btn; 
    TextView tv; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     tv = (TextView) findViewById(R.id.textView1); 
     btn = (Button) findViewById(R.id.button1); 
    } 

    public void getThis(View v) { 
     String str = "35" + Build.BOARD.length() % 10 + Build.BRAND.length() 
      % 10 + Build.CPU_ABI.length() % 10 + Build.DEVICE.length() % 10 
      + Build.DISPLAY.length() % 10 + Build.HOST.length() % 10 
      + Build.ID.length() % 10 + Build.MANUFACTURER.length() % 10 
      + Build.MODEL.length() % 10 + Build.PRODUCT.length() % 10 
      + Build.TAGS.length() % 10 + Build.TYPE.length() % 10 
      + Build.USER.length() % 10; 
     tv.setText(str); 
     UploadUniqueID uni = new UploadUniqueID(this, str); 
     uni.execute(str); 
    } 

    class UploadUniqueID extends AsyncTask<String, Integer, String> { 
     Context context; 
     MainActivity ma; 
     ProgressDialog dialog; 
     String id; 

     public UploadUniqueID(MainActivity activity, String str) { 
      ma = activity; 
      context = activity; 
      dialog = new ProgressDialog(activity); 
      id = str; 
     } 

     protected void onPreExecute() { 
      dialog.setMessage("Progress start"); 
      dialog.setCancelable(true); 
      dialog.show(); 
     } 

     @Override 
     protected String doInBackground(String... params) { 
      // perform long running operation operation 
      String id = params[0]; 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(
       "http://10.0.2.2/UniqueIdApp/myPHP.php"); 
      try { 
       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
        1); 
       nameValuePairs.add(new BasicNameValuePair("android", id)); 
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       httpclient.execute(httppost); 
      } catch (Exception e) { 
       Log.i("HTTP Failed", e.toString()); 
      } 

      return null; 
     } 

     protected void onProgressUpdate(Integer... integers) { } 

     protected void onPostExecute(String p_result) { 
      super.onPostExecute(p_result);   
      tv.setText("Sent"); 
      if (dialog.isShowing()) { 
       dialog.dismiss(); 
      } 
     } 
    } 
} 
+0

亲爱的,只是注解,这是用来告诉当我们想要重写一个方法时,我们可以使用这个注释来告诉编译器我们正在覆盖现有的方法。如果编译器发现在超类中找不到匹配的方法,则会生成警告。当我们重写一个方法时,这不是强制使用@Override。虽然它不是强制性的,但它被认为是最佳实践。 – BBdev

+0

@BBdev - 是的,我知道它不是强制性的,但如果你想关闭进度对话框,那么你必须调用它。否则编译器将无法检测到它,并且不会调用'dialog.dismiss();'。我发现只有这个问题,它的'onPostExecute'没有被叫做那个为什么我提出这个解决方案。 – GrIsHu

+1

这是吗?让我如果从'oncreate'方法中删除@Override并运行我的应用程序,你认为它不会启动我的应用程序?试试吧。据我所知,它会运行你的应用程序。 – BBdev