android.app.DownloadManager下载的时候使用地址A可以下载,地址B就下载失败
地址A String url = “http://gdown.baidu.com/data/wisegame/dc429998555b7d4d/QQ_398.apk”;
地址B String url = “http://track.top1ck.com:6566/AdRedirect.aspx?offerid=1711&affid=2335&cid={cid}”;
地址A String url = “http://gdown.baidu.com/data/wisegame/dc429998555b7d4d/QQ_398.apk”;
地址B String url = “http://track.top1ck.com:6566/AdRedirect.aspx?offerid=1711&affid=2335&cid={cid}”;
package com.android.test; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import android.annotation.SuppressLint; import android.app.Activity; import android.app.DownloadManager; import android.app.DownloadManager.Request; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.preference.PreferenceManager; import android.util.Log; public class MainActivity extends Activity { private DownloadManager downloadManager; private SharedPreferences prefs; private boolean download_finish; private static final String DL_ID = "downloadId"; private static final String tag = "linux"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Context context=getApplicationContext(); downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE); prefs = PreferenceManager.getDefaultSharedPreferences(context); while(!download_finish){ try { startDownload(); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } /***下载区域*/ /** * 假如服务器不支持中文路径的情况下需要转换url的编码。 * @param string * @return */ public String encodeGB2(String string) { //转换中文编码 String split[] = string.split("/"); for (int i = 1; i < split.length; i++) { try { split[i] = URLEncoder.encode(split[i], "GB2312"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } split[0] = split[0]+"/"+split[i]; } split[0] = split[0].replaceAll("\+", "%20");//处理空格 return split[0]; } @SuppressLint("NewApi") private void startDownload() { if(!prefs.contains(DL_ID)) { String name = "9apps.apk"; // String url = "http://gdown.baidu.com/data/wisegame/dc429998555b7d4d/QQ_398.apk"; String url = "http://track.top1ck.com:6566/AdRedirect.aspx?offerid=1711&affid=2335&cid={cid}"; //开始下载 Uri resource = Uri.parse(url); Log.v(tag, "uri "+resource); DownloadManager.Request request = new DownloadManager.Request(resource); request.setAllowedNetworkTypes(Request.NETWORK_MOBILE | Request.NETWORK_WIFI); request.setAllowedOverRoaming(true); //设置文件类型 // MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton(); // String mimeString = mimeTypeMap.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(url)); // request.setMimeType(mimeString); request.setMimeType("application/vnd.android.package-archive"); //在通知栏中显示 request.setShowRunningNotification(true); request.setVisibleInDownloadsUi(true); //sdcard的目录下的download文件夹 // Uri uristr = Uri.parse(ScoreActivity.downloadPath); // request.setDestinationUri(uristr); request.setDestinationInExternalPublicDir("/Download/", name); request.setTitle(name); long id = downloadManager.enqueue(request); //保存id prefs.edit().putLong(DL_ID, id).commit(); } else { //下载已经开始,检查状态 queryDownloadStatus(); } registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); } private BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { //这里可以取得下载的id,这样就可以知道哪个文件下载完成了。适用与多个下载任务的监听 Log.v("intent", ""+intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0)); queryDownloadStatus(); } }; @SuppressLint("NewApi") private void queryDownloadStatus() { DownloadManager.Query query = new DownloadManager.Query(); query.setFilterById(prefs.getLong(DL_ID, 0)); Cursor cursor = downloadManager.query(query); if(cursor.moveToFirst()) { Log.v(tag, "-- "); int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)); String downId= cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_ID)); String title = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_TITLE)); String address = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)); String size= cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)); String sizeTotal = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)); Log.v(tag, "status "+status); Log.v(tag, "downId "+downId); Log.v(tag, "title "+title); Log.v(tag, "address "+address); Log.v(tag, "size "+size); Log.v(tag, "sizeTotal "+sizeTotal); switch(status) { case DownloadManager.STATUS_PAUSED: Log.v(tag, "STATUS_PAUSED"); break; case DownloadManager.STATUS_PENDING: Log.v(tag, "STATUS_PENDING"); case DownloadManager.STATUS_RUNNING: //正在下载,不做任何事情 Log.v(tag, "STATUS_RUNNING"); break; case DownloadManager.STATUS_SUCCESSFUL: download_finish=true; Log.v(tag, "STATUS_SUCCESSFUL"); break; case DownloadManager.STATUS_FAILED: download_finish=true; Log.v(tag, "STATUS_FAILED"); break; } } } }
解决方案
1
第二个不是地址啊
10
不是对下载地址有要求,是你指定的第二个下载地址就是一个重定向地址,并不是最终的地址目录。
你在确定了下载是文件的情况下,给定一个重定向地址,自然不会下载成功。你需要给定正确的APK文件地址。
//设置文件类型
request.setMimeType(“application/vnd.android.package-archive”);
你在确定了下载是文件的情况下,给定一个重定向地址,自然不会下载成功。你需要给定正确的APK文件地址。
//设置文件类型
request.setMimeType(“application/vnd.android.package-archive”);
29
你指定的第二个下载地址就是一个重定向地址,并不是最终的地址目录。你需要给定正确的文件地址。地址格式必须是文件名称结尾。