我想从http获取json(视频详细信息)数据并添加到数组列表中,以便根据用户选择的类别在活动的列表视图中显示。 但我想在从互联网上获取数据时显示进度对话框
public class GetOnlineData extends AsyncTask<String, Integer, ArrayList<McuVideo>> {
private Activity activity;
private ArrayList<McuVideo> arrayvid;
private String sdcard_path;
private ProgressDialog pdialog;
public GetOnlineData(Activity activity)
{
this.activity=activity;
sdcard_path=Environment.getExternalStorageDirectory()+File.separator
+"McuVideos"; arrayvid=new ArrayList<McuVideo>();
}
@Override
protected void onPreExecute()
{
super.onPreExecute();
pdialog = new ProgressDialog(activity);
Log.d("pre", "pre");
pdialog.setMessage("Please waiit");
pdialog.setTitle("Loading");
pdialog.setIndeterminate(true);
pdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// dialog.setCancelable(false);
// dialog.setCanceledOnTouchOutside(false);
pdialog.show();
}
@Override
protected void onPostExecute(ArrayList<McuVideo> result)
{
super.onPostExecute(result);
if(pdialog.isShowing())
pdialog.dismiss();
Log.d("post", "post");
}
@Override
protected void onProgressUpdate(Integer... progress)
{
super.onProgressUpdate(progress);
pdialog.setProgress(progress[0]);
Log.d("pupdate", ""+progress[0]);
}
@Override
protected ArrayList<McuVideo> doInBackground(String... address) {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(address[0]);
try{
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if(statusCode == 200){
HttpEntity entity = response.getEntity();
//Log.d("context length",""+entity.getContentLength());
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
int i=0;
while((line = reader.readLine()) != null){
builder.append(line);
Log.d("line length",""+line.length());
i++;
// publishProgress(builder.length());
}
} else {
MyToast.makeToast(activity,"failed to load data",MyToast.ERROR, MyToast.DURATION_LONG).show();
Log.e(MainActivity.class.toString(),"Failedet JSON object");
//super.onCancelled();
cancel(true);
}
} catch (Exception e){
Toast.makeText(activity,e.toString(), Toast.LENGTH_LONG).show();
}
try{
if(builder.length()>15)
{
// Log.d("query",query[0]);
JSONObject jsonRootObject = new JSONObject(builder.toString());
JSONArray jsonArray = jsonRootObject.optJSONArray("videos");
for(int i=1; i <jsonArray.length(); i++)
{
JSONObject jsonObject = jsonArray.getJSONObject(i);
Long vid =Long.parseLong(jsonObject.optString("vid").toString());
String title = jsonObject.optString("title").toString();
//String path =context.getResources().getString(R.string.base_web_url)+ jsonObject.optString("path").toString();
String description = jsonObject.optString("description").toString();//.substring(0, 20);
String path = jsonObject.optString("path").toString();
// String duration=jsonObject.optString("duration").toString();
String duration=jsonObject.optString("upload_date").toString();
String category=jsonObject.optString("category").toString();
// URL url = new URL(context.getResources().getString(R.string.web_url)+"/thumnails/"+vid+".jpg");
// Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
//String thumnail_path=context.getResources().getString(R.string.web_url)+"/thumnails/"+vid+".jpg";
// String thumnail_path="http://sugamgroups.com/mcu/thumnails/"+vid+".jpg";
// String thumnail_path=GlobalData.t
URL url = new URL (activity.getApplicationContext().getResources().getString(R.string.base_web_url)+"/thumbnails/"+vid+".jpg");
// URL url = new URL(thumnail_path);
File file = new File(sdcard_path+File.separator+vid+".jpg");
// long startTime = System.currentTimeMillis();
// Log.d("DownloadFile", "Begin Download URL: " + url + " Filename: " + fileName);
try
{
URLConnection ucon = url.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1)
baf.append((byte) current);
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.flush();
fos.close();
}
catch(Exception e)
{
Log.e("1",e.toString());
}
// Toast.makeText(activity, "file download",Toast.LENGTH_LONG).show();
//bmp.compress(Bitmap.CompressFormat.JPEG ,Bitmap., stream)*/
arrayvid.add(new McuVideo(vid,path,duration,title,description,category));
// Thread.sleep(1000);
publishProgress((int)i*100/jsonArray.length());
}
}
}
catch(Exception e)
{
// Log.e(GetOnlineVideos.this.toString(), e.toString());
MyToast.makeToast(activity, e.toString(), MyToast.ERROR, MyToast.DURATION_LONG).show();
}
return arrayvid;
}
}
答案 0 :(得分:0)
尝试以下内容: 1.评论这一行
//pdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
2。检查您是否将progressvalue作为正整数。记录进度并检查
publishProgress((int)i*100/jsonArray.length());