我有与mysql数据库交互并下载数据和图片的代码。因此,当我运行此代码时,下载数据需要一些时间,我的手机屏幕会冻结片刻,有时会变黑。我在此代码之前放了一个ProgressDialog,但它在下载数据后出现。现在我使用AsyncTask类,但它仍然没有显示ProgressDialog
我打电话给这个班:myarray = new Download(getApplicationContext()).doInBackground("my php file URL");
这是班级:
class Download extends AsyncTask<String, String, MyArray>
{
ProgressDialog progressdialog;
Context baseconext;
public Download(Context baseconext)
{
this.baseconext = baseconext;
}
@Override
protected MyArray doInBackground(String... params) {
// TODO Auto-generated method stub
MyArray myarray = new MyArray();
myarray = getdata("http://www.neginmelk.ir/app/getnews.php");
myarray = getpic(myarray);
return myarray;
}
@Override
protected void onPostExecute(MyArray result) {
// TODO Auto-generated method stub
progressdialog = ProgressDialog.show(baseconext, "dialog title",
"dialog message", true);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
progressdialog.dismiss();
}
@Override
protected void onProgressUpdate(String... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}
public MyArray getpic(MyArray temparray)
{
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
for (int i = 0; i < temparray.arrpath.length; i++)
{
System.out.println(temparray.arrpath[i]);
}
for (int i = 0; i < temparray.arrpath.length; i++)
{
if(!temparray.arrpath[i].equalsIgnoreCase("null"))
{
try {
URL url = new URL(temparray.arrpath[i].toString());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(input);
temparray.arrpic.add(bitmap);
} catch (Exception e) {
// TODO: handle exception
//Toast.makeText(getApplicationContext(), "error ocures", Toast.LENGTH_LONG).show();
}
}
else
{
temparray.arrpic.add(null);
}
}
return temparray;
}
public MyArray getdata(String src)
{
InputStream is = null;
String line,result = null;
MyArray myarray = new MyArray();
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
try {
HttpClient httpClient= new DefaultHttpClient();
HttpPost httpPost=new HttpPost(src);
// httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response =httpClient.execute(httpPost);
HttpEntity entity= response.getEntity();
is=entity.getContent();
} catch (ClientProtocolException e) {
//Toast.makeText(this,"exeption client", Toast.LENGTH_LONG).show();
} catch (IOException e) {
// TODO Auto-generated catch block
//Toast.makeText(this,"exeption io", Toast.LENGTH_LONG).show();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
while((line = reader.readLine())!=null)
sb.append(line + "\n");
result = sb.toString();
is.close();
System.out.println("********************************************my************");
System.out.println(result);
//Toast.makeText(this,"asdasdadsadsads", Toast.LENGTH_LONG).show();
} catch (Exception e) {
//Toast.makeText(Description.this,"exeption 2", Toast.LENGTH_LONG).show();
}
//////////////////json to array
try {
String title = "",content="",imgpath = "";
JSONArray jArray = new JSONArray(result);
int count = jArray.length();
for (int i = 0; i <count; i++)
{
JSONObject json_data = jArray.getJSONObject(i);
title += json_data.getString("title")+ ":";
content += json_data.getString("content")+ ":";
imgpath += json_data.getString("imgpath")+ "#";
}
System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<,return from get data");
System.out.println(imgpath);
myarray.arrtitle = title.split(":");
myarray.arrcontent = content.split(":");
myarray.arrpath = imgpath.split("#");
System.out.println("///////////////////////////////////////////////////data");
System.out.println(myarray.arrpath);
} catch (Exception e) {
//Toast.makeText(this,"exeption last", Toast.LENGTH_LONG).show();
}
//Toast.makeText(Description.this, "we r returning", Toast.LENGTH_LONG).show();
return myarray;
}
}
答案 0 :(得分:0)
在调用asynctask之前在java文件中显示progressdialog:
pDialog = ProgressDialog.show(YourActivityName.this,"","Creating New Card...", true);
pDialog.setCancelable(true);
然后在你的ASyncTask中,在onPostExecute中解除它。
@Override
protected void onPostExecute(MyArray result) {
// TODO Auto-generated method stub
pDialog.dismiss();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
答案 1 :(得分:0)
像这样使用:
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
progressdialog = ProgressDialog.show(baseconext, "dialog title",
"dialog message", true);
}
并在后期执行:
@Override
protected void onPostExecute(MyArray result) {
progressdialog.dismiss();
}
答案 2 :(得分:0)
Sajjad,您需要在活动中定义进度对话框。
private ProgressDialog prgDialog;
然后在AsycTask中,你需要写下面的代码:
private class PrefetchData extends AsyncTask<Void, Integer, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
prgDialog = new ProgressDialog(YourActivityName.this);
prgDialog.setMessage("Downloading Data. Please wait...");
prgDialog.setIndeterminate(false);
prgDialog.setMax(100);
prgDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
prgDialog.setCancelable(false); //depends on you
prgDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
InputStream input = null;
OutputStream output = null;
HttpURLConnection connection = null;
try {
URL url = new URL(Constant.jsonCall);
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.connect();
// expect HTTP 200 OK, so we don't mistakenly save error report
// instead of the file
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
// return Error
}
int fileLength = connection.getContentLength();
input = connection.getInputStream();
byte data[] = new byte[4096];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
if (fileLength > 0){
publishProgress((int) (total * 100 / fileLength));
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (output != null)
output.close();
if (input != null)
input.close();
} catch (IOException ignored) {
}
if (connection != null)
connection.disconnect();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
//do your work
}
//Update the progress
@Override
protected void onProgressUpdate(Integer... values)
{
prgDialog.setProgress(values[0]);
}
}