2013-11-20 3 views
-2

내 앱에서 weather xml api를 구문 분석하려고하지만 API의 URL을 구문 분석하는 동안 예외가 발생합니다. 다음은 logcat의 스크린 샷입니다. 예외가 라인 (654)에서 발생하는 로그 캣 관찰에서 안드로이드에서 URL 구문 분석에 예외가 발생합니다. Java

enter image description here

와 나는 내가 issue.You 아래 코드에서 문을 확인할 수 있습니다 해결 방법을 알고 해달라고, 라인 (654)에 xr.parse(new InputSource(url.openStream())); 문을 가지고 있지만. 이 점에서 저를 도우십시오. 귀하의 답변은 높이 평가 될 것입니다. 미리 감사드립니다.

여기 내 코드입니다.

private void parseWeatherURL(String strURL) 
{ 
    try 
    { 
     String weatherURL="http://api.worldweatheronline.com/free/v1/weather.ashx?key=476yv2t87x67j5pzb7hbazn6&q=34.25,71.014&cc=yes&num_of_days=5&format=xml"; 
     /* Replace blanks with HTML-Equivalent. */ 
     URL url = new URL(weatherURL.replace(" ", "%20")); 
     Log.e("Weather", "URL"); 

     /* Get a SAXParser from the SAXPArserFactory. */ 
     SAXParserFactory spf = SAXParserFactory.newInstance(); 
     SAXParser sp = spf.newSAXParser(); 
     Log.e("Weather", "SAX"); 

     /* Get the XMLReader of the SAXParser we created. */ 
     XMLReader xr = sp.getXMLReader(); 

     com.ifahja.weather.WeatherHandler myWeatherHandler = new com.ifahja.weather.WeatherHandler(); 
     xr.setContentHandler(myWeatherHandler); 
     Log.e("Weather", "Handler"); 
     /* Parse the xml-data our URL-call returned. */ 
     xr.parse(new InputSource(url.openStream())); 

     //Log.d(TAG, "it's all right"); 

     Log.e("Weather URL", "it's all right"); 

    } 
    catch(Exception e) 
    { 
     Log.e("Problem", "parseWeatherURL()"); 
     e.printStackTrace(); 
    } 
} 

답변

1

UI 스레드 외부에서 네트워크를 호출하고 있습니까? NetworkOnMainThreadException이 있으므로 ...

그렇지 않은 경우 AsyncTask를 사용하십시오. 예를 들면 :

또는

당신은 안드로이드 비동기 HTTP 클라이언트 라이브러리를 사용할 수 있습니다. 아주 간단한 라이브러리, 작은 크기 (25kb), 사용하기 쉽고 비동기. 코드는 다음과 같습니다.

AsyncHttpClient client = new AsyncHttpClient(); 
client.get("http://www.dfsdfsdfsdfsd.com", new AsyncHttpResponseHandler() { 
    @Override 
    public void onSuccess(String response) { 
     // Here is your content ! 
    } 
}); 

사용 가능 here.


그렇지 않으면, 당신은 별도의 전화로 전화를 잘라 널 (null) 값을 확인 할 수 있습니다

URLConnection urlcon = url.openConnection(); 
urlcon.setReadTimeout(10000); 
InputStream is = urlcon.getInputStream(); 
관련 문제