2011-05-15 4 views
1

내 응용 프로그램은 주 스레드에서 네트워크 호출을하기 때문에 실패하고 있다는 것을 제외하고는 모든 Android 버전에서 정상적으로 작동합니다. 내 코드는 url.openStream()에 실패하고 나는이 스레드를 별도로 호출해야한다고 생각한다. 또한이 클래스는 Utilities이므로 응용 프로그램의 많은 부분에서 사용하는 함수를 참조 할 수 있습니다. 나는 이것이 잘못되었다는 것을 확신하지만, 당시 내가 아는 것이 전부였다. 누군가가 시간을내어 기꺼이이 일을 어떻게 수행 했어야하고 openStream 전화를 AsyncTask 전화로 감쌀 수 있는지 보여 주길 바란다. 귀하의 도움에 미리 감사드립니다. 또한, 나는 지난 며칠 동안 혼자서 알아 내려고 노력했다.Android Help with AsyncTask

class Utilities { 
    static XMLReader xr; 
    static URL url; 
    private final static int CHUNK_SIZE = 32 * 1024; 
    static byte[] _fileIOBuffer = new byte[CHUNK_SIZE]; 

static String DBGetOnlineVersionNumber(final Activity activity){ 

    final String version = null; 

    try { 

     /* Create a URL we want to load some xml-data from. */ 
     String location = activity.getString(R.string.version_file_location); 
     url = new URL(location); 

     /* Get a SAXParser from the SAXPArserFactory. */ 
     SAXParserFactory spf = SAXParserFactory.newInstance(); 
     SAXParser sp = spf.newSAXParser(); 

     /* Get the XMLReader of the SAXParser we created. */ 
     xr = sp.getXMLReader(); 
     /* Create a new ContentHandler and apply it to the XML-Reader*/ 
     final XMLVersionReader myExampleHandler = new XMLVersionReader(); 
     xr.setContentHandler(myExampleHandler); 

     xr.parse(new InputSource(url.openStream())); //Fails here 
     /* Parsing has finished. */ 
    } catch (Exception e) { 
     System.out.println("XML Pasing Excpetion = " + e); 
    } 
    return version; 
} 
+0

실제로 원하는 모든 것이 문자열 인 경우 활동 참조를 전달하는 것은 좋지 않은 생각입니다. – dmon

답변

1

이제 코드를 별도의 정적 방법으로 배치 했으므로 대신 사용하는 것이 더 적절하다고 생각합니다. 동 기적으로 또는 동일한 코드를 호출 할 수있는 가능성을 남겨두고 별도의 스레드에서 호출 할 수 있습니다. 호출을 AsyncTask로 랩핑하면 액티비티에 추가 된 경우이 주변의 어딘가에서 수행 할 수 있습니다.

new AsyncTask<Activity, Void, String>() { 
    @Override 
    public String doInBackground(Activity... activities) { 
     return Utilities.DBGetOnlineVersionNumber(activities[0]); 
    } 
    @Override 
    public void onPostExecute(String result) { 
     Log.d("VERSION_NUMBER", result); 
    } 
}.execute(this); 
+0

불만을 표시하고 알려주세요. 빠른 답변 감사합니다. –

+0

허니 콤에 대한 문제가 해결 될 것이라고 약속 할 수는 없지만. 그러나 이것은 제가 자주하는 일입니다. 동기 메서드를 작성하고, 필요한 경우 별도의 스레드에서 호출하십시오. – harism

+0

잘 작동합니다. 도움과 빠른 답변을 보내 주셔서 감사합니다. –