从PreferenceFragment发送http-get(live-wallpaper)

时间:2013-03-05 01:17:13

标签: android http libgdx android-preferences live-wallpaper

我创建了一个壁纸(适用于我的Android 4.0.3),但我对首选项有疑问。

当我尝试从http-get调用服务PreferenceFragment时,我没有得到任何回复。在实践中,似乎呼叫丢失了。我尝试过像:

这样的电话
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);
  }
}

1 个答案:

答案 0 :(得分:1)

您可能会获得NetworkOnMainThreadException,因为您在PreferenceFragment中建立了一个网络连接,该连接在主UI线程上运行。

如果是这样,您可以将网络作业移至后台服务。请注意,Service也在主UI线程上运行。因此,您需要在服务中使用Thread

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

try {
    // ...
} catch (Exception e) {
    // ... => HERE
}