带有进度条的Android应用程序下载程序?

时间:2014-07-13 17:05:17

标签: android

我正在尝试将游戏的mod文件下载到特定文件夹,我遇到了一些麻烦。我已经制作了下载代码,但它崩溃了应用程序,我想让进度条继续下载,但不太确定如何。这是我的代码:

package com.dogger20011.mcpemodlocater;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class v090 extends Activity {
/** Called when the activity is first created. */

Button button;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.v090);

    button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DownloadFromUrl();
        }
    });
}

public void DownloadFromUrl() {
    try {

        URL url = new URL("https://docs.google.com/uc? 
authuser=0&id=0B6ocq4LWhNJkRlpXelpheGtnZUE&export=download");
        HttpURLConnection c = (HttpURLConnection) url.openConnection();
        c.setRequestMethod("GET");
        c.setDoOutput(true);
        c.connect();

        String Path = Environment.getExternalStorageDirectory() +      
"sdcard/Android/data/com.snowbound.pockettool.free/Patches/";
        Log.v("PortfolioManger", "PATH: " + Path);
        File file = new File(Path);
        file.mkdirs();
        FileOutputStream fos = new FileOutputStream("shader mod.mod");

        InputStream is = c.getInputStream();

        byte[] buffer = new byte[702];
        int len1 = 0;
        while ((len1 = is.read(buffer)) != -1) {
            fos.write(buffer, 0, len1);
        }
        fos.close();
        is.close();
    } catch (IOException e) {
        Log.d("PortfolioManger", "Error: " + e);
    }
    Log.v("PortfolioManger", "Check: ");
}

}

07-13 16:13:40.537:E / AndroidRuntime(1084):致命异常:主要 07-13 16:13:40.537:E / AndroidRuntime(1084):进程:com.dogger20011.mcpemodlocater,PID:1084 07-13 16:13:40.537:E / AndroidRuntime(1084):android.os.NetworkOnMainThreadException 07-13 16:13:40.537:E / AndroidRuntime(1084):在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145) 07-13 16:13:40.537:E / AndroidRuntime(1084):at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 07-13 16:13:40.537:E / AndroidRuntime(1084):at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 07-13 16:13:40.537:E / AndroidRuntime(1084):at java.net.InetAddress.getAllByName(InetAddress.java:214) 07-13 16:13:40.537:E / AndroidRuntime(1084):at com.android.okhttp.internal.Dns $ 1.getAllByName(Dns.java:28) 07-13 16:13:40.537:E / AndroidRuntime(1084):at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216) 07-13 16:13:40.537:E / AndroidRuntime(1084):at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122) 07-13 16:13:40.537:E / AndroidRuntime(1084):at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292) 07-13 16:13:40.537:E / AndroidRuntime(1084):at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255) 07-13 16:13:40.537:E / AndroidRuntime(1084):at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206) 07-13 16:13:40.537:E / AndroidRuntime(1084):at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345) 07-13 16:13:40.537:E / AndroidRuntime(1084):at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89) 07-13 16:13:40.537:E / AndroidRuntime(1084):at com.android.okhttp.internal.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:161) 07-13 16:13:40.537:E / AndroidRuntime(1084):at com.dogger20011.mcpemodlocater.v090.DownloadFromUrl(v090.java:43) 07-13 16:13:40.537:E / AndroidRuntime(1084):at com.dogger20011.mcpemodlocater.v090 $ 1.onClick(v090.java:31) 07-13 16:13:40.537:E / AndroidRuntime(1084):在android.view.View.performClick(View.java:4438) 07-13 16:13:40.537:E / AndroidRuntime(1084):在android.view.View $ PerformClick.run(View.java:18422) 07-13 16:13:40.537:E / AndroidRuntime(1084):在android.os.Handler.handleCallback(Handler.java:733) 07-13 16:13:40.537:E / AndroidRuntime(1084):在android.os.Handler.dispatchMessage(Handler.java:95) 07-13 16:13:40.537:E / AndroidRuntime(1084):在android.os.Looper.loop(Looper.java:136) 07-13 16:13:40.537:E / AndroidRuntime(1084):在android.app.ActivityThread.main(ActivityThread.java:5017) 07-13 16:13:40.537:E / AndroidRuntime(1084):at java.lang.reflect.Method.invokeNative(Native Method) 07-13 16:13:40.537:E / AndroidRuntime(1084):at java.lang.reflect.Method.invoke(Method.java:515) 07-13 16:13:40.537:E / AndroidRuntime(1084):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) 07-13 16:13:40.537:E / AndroidRuntime(1084):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 07-13 16:13:40.537:E / AndroidRuntime(1084):at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

从你的logcat中可以看出 -

  

android.os.NetworkOnMainThreadException 07-13 16:13:40.537:

- 您正在主线程上运行网络代码。 Connecting to the Network培训指南解释了网络操作应该在单独的线程上执行。

  

网络运营可能涉及不可预测的延迟。为防止此操作导致糟糕的用户体验,请始终在UI的单独线程上执行网络操作。

您也可以在链接中找到有关正确方法的信息。