0

我创建了一个壁纸(适用于我的 Android 4.0.3),但我的偏好有问题。

当我尝试从 调用服务http-getPreferenceFragment,我没有得到任何响应。在实践中,似乎呼叫丢失了。我试过这样的电话:

InputStream stream = null;
StringBuilder fos = null;
try {
    URL url = new URL("http://.....");

    stream = url.openConnection().getInputStream();
    InputStreamReader reader = new InputStreamReader(stream);

    fos = new StringBuilder();
    int next = -1;
    while ((next = reader.read()) != -1) {
        fos.append((char)next);
    }
} catch (Exception e) {
    // ...
} finally {
    // ...
}

或使用外部框架,例如libgdx

HttpRequest httpRequest = new HttpRequest(HttpMethods.GET);
httpRequest.setUrl("http://.....");
NetJavaImpl net = new NetJavaImpl();
net.sendHttpRequest(httpRequest, this);

但什么也没发生。在设备上没有关于此的任何痕迹或存在概率异常。

我有这样的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="it.mytest.live"
    android:installLocation="preferExternal"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.SET_WALLPAPER" />

    <uses-feature
        android:name="android.software.live_wallpaper"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MyWallpaperSettings"
            android:exported="true"
            android:hardwareAccelerated="false"
            android:label="MyWallpaperSettings"
            android:permission="android.permission.INTERNET" />

        <service
            android:name=".MyWallpaper"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:permission="android.permission.BIND_WALLPAPER" >
            <intent-filter>
                <action android:name="android.service.wallpaper.WallpaperService" />
            </intent-filter>

            <meta-data
                android:name="android.service.wallpaper"
                android:resource="@xml/livewallpaper"
                android:value="true" />
        </service>
    </application>

</manifest>

你有什么主意吗?我要疯了 :(

*编辑:已解决,谢谢!!*

private class SendAsyncTask extends AsyncTask<String, String, String>
{
  protected String doInBackground(String... status)
  {
     String result = "";
     try
     {
        String url = "my magic http-get";

        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = httpclient.execute(new HttpGet(url));
        if(response != null && response.getEntity() != null)
        {
           InputStream stream = response.getEntity().getContent();

           try
           {
              // parse an XML with libgdx ...
              XmlReader xmlReader = new XmlReader();
              Element root = xmlReader.parse(stream);

              Array<Element> childs = root.getChildrenByNameRecursively("mykids");
              for (Element child: childs)
              {
                 Element myelement = child.getChildByName("name");
                 //do something...
              }
           }
           catch (Exception e)
           {
              //
           }
           finally
           {
              try
              {
                 stream.close();
              }
              catch (Exception e)
              {
                 //
              }
           }
        }
     }
     catch (Exception e)
     {
       //
     }

     return result;
  }

  protected void onPostExecute(String result)
  {
     super.onPostExecute(result);
  }
}
4

1 回答 1

1

您可能会得到 a NetworkOnMainThreadException,因为您在 a 中建立了网络连接PreferenceFragment,它在主 UI 线程上运行。

如果是这样,您可以将网络作业移动到后台服务。请注意,Service它也在主 UI 线程上运行。所以你需要Thread里面的服务。

附注:您似乎吞下了所有例外情况:

try {
    // ...
} catch (Exception e) {
    // ... => HERE
}
于 2013-03-05T01:50:50.217 回答