0

これは私の最初の投稿であり、うまくいくことを願っています...

Open Source Updater (YAOS Updater) を編集しようとしていますが、接続しようとするとエラーが発生します。

できる限りうまく説明しようと思います。

すべてのファイルを一覧表示するために使用される JSON に値を追加しました。各ファイルの新しい値は、ファイルの完全な URL を含む urlArchivo です。

これはコードの一部です:

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 throws android.os.NetworkOnMainThreadException because...

The exception that is thrown when an application attempts to perform a networking operation on its main thread.

This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it's heavily discouraged.

Create a separate thread for your networking operations, it's telling you that basically.

Potentially long running operations such as network or database operations, or computationally expensive calculations such as resizing bitmaps should be done in a child thread

Note that services, like other application objects, run in the main thread of their hosting process. This means that, if your service is going to do any CPU intensive (such as MP3 playback) or blocking (such as networking) operations, it should spawn its own thread in which to do that work.

Source1 Source2 Source3

You could have investigated this by yourself, but eh - do it next time.

于 2012-08-19T13:17:55.003 に答える