2013-03-05 1 views
0

배경 화면을 만들었지 만 (Android 4.0.3 용) 기본 설정에 문제가 있습니다.PreferenceFragment (라이브 배경 화면)에서 http-get 보내기

서비스 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); 
    } 
} 
+0

당신이 UI 스레드에서이 작업을 수행하려고 : 당신이 모든 예외를 삼킨 것 같다? –

답변

1

의 메인 UI에서 실행되는 PreferenceFragment에서 네트워크 연결을 만들었 기 때문에 당신은 아마도 NetworkOnMainThreadException를 얻을 수 스레드.

그렇다면, 당신은 백그라운드 서비스에 네트워크 작업을 이동할 수 있습니다. 너무 메인 UI 스레드에서 그 Service 실행을합니다. 그래서 당신은 서비스 내에서 Thread이 필요합니다.

,

측면 참고 :

try { 
    // ... 
} catch (Exception e) { 
    // ... => HERE 
} 
관련 문제