0

这是我在这里的第一篇文章,我希望我能做得很好......

我正在尝试编辑开源更新程序(YAOS Updater),当我尝试连接时,它给了我一个错误。

我会尽力解释得更好。

我在用于列出所有文件的 JSON 中添加了一个值。每个文件的新值为 urlArchivo,其中包含文件的完整 url。

这是代码的一部分:

package org.yaosupdater.services;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import org.yaosupdater.R;

import android.annotation.TargetApi;
import android.app.DownloadManager;
import android.app.DownloadManager.Query;
import android.app.DownloadManager.Request;
import android.app.Service;
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.os.IBinder;
import android.util.Log;
import android.widget.Toast;

@TargetApi(16)
public class DownloadService extends Service{

private DownloadManager dm;
private String nombre;
private String archivo;
private String urlArchivo;
private String[] mirrors;
private long enqueue;

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onStart(Intent intent, int startid){
    super.onStart(intent, startid);
    dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    Bundle bundle = intent.getExtras();
    nombre = (String) bundle.get("nombre");
    Log.d("Dexafree", "Nombre: " +nombre);
    archivo = (String) bundle.get("archivo");
    Log.d("Dexafree", "Archivo: " +archivo);
    urlArchivo = (String) bundle.getString("urlArchivo");
    Log.d("Dexafree", "urlArchivo: " +urlArchivo);
    mirrors = (String[]) bundle.get("mirrors");
    URL url;
    int j = 0;
    boolean found = false;
    while((j < mirrors.length)&&(!found)){
        try {
            url = new URL(urlArchivo);
            Log.d("Dexafree", "Intentando conectar a: " +urlArchivo);
            //url = new URL(urlArchivo);
            //Log.d("Dexafree", "Intentando conectar a: " +urlArchivo);
            URLConnection urlCon = url.openConnection();
            Log.d("Dexafree", "Conexion abierta a: " +urlCon);
            Log.d("Dexafree", "Debe ser igual a: " + urlArchivo);
            InputStreamReader is = new InputStreamReader(urlCon.getInputStream());
            found = true;
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            Log.d("MIUIEsUpdater", "El archivo " +urlArchivo +" no se encontró.");
            found = false;
        }
        j++;
    }   
    j--;

logcat 中的变量 urlArchivo 获取正确的 URL,我的意思是,

    Log.d("Dexafree", "Intentando conectar a: " +urlArchivo);

返回文件正确的完整 url,但它是 FC,显示下一个 logcat

08-19 14:51:40.417: E/Trace(4134): error opening trace file: No such file or directory (2)
08-19 14:51:40.449: D/Dexafree(4134): Nombre: MODEM KF1
08-19 14:51:40.453: D/Dexafree(4134): Archivo: Modem_KF1.zip
08-19 14:51:40.453: D/Dexafree(4134): urlArchivo: http://content.wuala.com/contents/elitemovil/Galaxy_S/GT-I9000/carpetaprueba/Modem_KF1.zip
08-19 14:51:40.453: D/Dexafree(4134): Intentando conectar a: http://content.wuala.com/contents/elitemovil/Galaxy_S/GT-I9000/carpetaprueba/Modem_KF1.zip
08-19 14:51:40.453: D/Dexafree(4134): Conexion abierta a: libcore.net.http.HttpURLConnectionImpl:http://content.wuala.com/contents/elitemovil/Galaxy_S/GT-I9000/carpetaprueba/Modem_KF1.zip
08-19 14:51:40.453: D/Dexafree(4134): Debe ser igual a: http://content.wuala.com/contents/elitemovil/Galaxy_S/GT-I9000/carpetaprueba/Modem_KF1.zip
08-19 14:51:40.457: D/AndroidRuntime(4134): Shutting down VM
08-19 14:51:40.457: W/dalvikvm(4134): threadid=1: thread exiting with uncaught exception (group=0x40edb300)
08-19 14:51:40.500: E/AndroidRuntime(4134): FATAL EXCEPTION: main
08-19 14:51:40.500: E/AndroidRuntime(4134): java.lang.RuntimeException: Unable to start service org.yaosupdater.services.DownloadService@416c0c38 with Intent { cmp=org.yaosupdater/.services.DownloadService (has extras) }: android.os.NetworkOnMainThreadException
08-19 14:51:40.500: E/AndroidRuntime(4134):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2507)
08-19 14:51:40.500: E/AndroidRuntime(4134):     at android.app.ActivityThread.access$1900(ActivityThread.java:130)
08-19 14:51:40.500: E/AndroidRuntime(4134):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
08-19 14:51:40.500: E/AndroidRuntime(4134):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-19 14:51:40.500: E/AndroidRuntime(4134):     at android.os.Looper.loop(Looper.java:137)
08-19 14:51:40.500: E/AndroidRuntime(4134):     at android.app.ActivityThread.main(ActivityThread.java:4745)
08-19 14:51:40.500: E/AndroidRuntime(4134):     at java.lang.reflect.Method.invokeNative(Native Method)
08-19 14:51:40.500: E/AndroidRuntime(4134):     at java.lang.reflect.Method.invoke(Method.java:511)
08-19 14:51:40.500: E/AndroidRuntime(4134):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-19 14:51:40.500: E/AndroidRuntime(4134):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-19 14:51:40.500: E/AndroidRuntime(4134):     at dalvik.system.NativeStart.main(Native Method)
08-19 14:51:40.500: E/AndroidRuntime(4134): Caused by: android.os.NetworkOnMainThreadException
08-19 14:51:40.500: E/AndroidRuntime(4134):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
08-19 14:51:40.500: E/AndroidRuntime(4134):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
08-19 14:51:40.500: E/AndroidRuntime(4134):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
08-19 14:51:40.500: E/AndroidRuntime(4134):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
08-19 14:51:40.500: E/AndroidRuntime(4134):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
08-19 14:51:40.500: E/AndroidRuntime(4134):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
08-19 14:51:40.500: E/AndroidRuntime(4134):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
08-19 14:51:40.500: E/AndroidRuntime(4134):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
08-19 14:51:40.500: E/AndroidRuntime(4134):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
08-19 14:51:40.500: E/AndroidRuntime(4134):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
08-19 14:51:40.500: E/AndroidRuntime(4134):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
08-19 14:51:40.500: E/AndroidRuntime(4134):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
08-19 14:51:40.500: E/AndroidRuntime(4134):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
08-19 14:51:40.500: E/AndroidRuntime(4134):     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
08-19 14:51:40.500: E/AndroidRuntime(4134):     at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
08-19 14:51:40.500: E/AndroidRuntime(4134):     at org.yaosupdater.services.DownloadService.onStart(DownloadService.java:73)
08-19 14:51:40.500: E/AndroidRuntime(4134):     at android.app.Service.onStartCommand(Service.java:450)
08-19 14:51:40.500: E/AndroidRuntime(4134):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2490)
08-19 14:51:40.500: E/AndroidRuntime(4134):     ... 10 more

我希望我已经发布了解决此问题所需的所有信息。如果需要更多信息,请告诉我!

4

1 回答 1

2

Android抛出android.os.NetworkOnMainThreadException,因为...

当应用程序尝试在其主线程上执行网络操作时引发的异常。

这仅针对面向 Honeycomb SDK 或更高版本的应用程序抛出。允许以早期 SDK 版本为目标的应用程序在其主事件循环线程上进行网络连接,但非常不鼓励这样做。

为您的网络操作创建一个单独的线程,它基本上告诉您。

可能长时间运行的操作(如网络或数据库操作)或计算量大的计算(如调整位图大小)应在子线程中完成

请注意,服务与其他应用程序对象一样,在其托管进程的主线程中运行。这意味着,如果您的服务要执行任何 CPU 密集型(例如 MP3 播放)或阻塞(例如网络)操作,它应该生成自己的线程来完成这项工作。

来源1 来源 2 来源 3

你本可以自己调查一下,但是嗯 - 下次再做。

于 2012-08-19T13:17:55.003 回答