마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

안드로이드 스튜디오에서 xml 데이터를 읽어오는 과정에서 IOException 에러가 납니다.

0 추천

wamp서버를 이용해서 htdocs에 있는 xml데이터를 읽어오려고 합니다

디버깅을 해보니 IOException으로 넘어가버리더라구요.

다른 학생들것과 비교를 해봤는데도 큰 차이점이 없구요.혹시 제 코드가 잘못된 부분이 있나요?

 

public void getData(View v)
{
    URL url = null; HttpURLConnection urlConnection;
    String strUrl = "http://10.0.2.2/wellsites/welldata.xml";
    try
    {
        url = new URL(strUrl);
        urlConnection = (HttpURLConnection) url.openConnection();
        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
        readStream(in);
        urlConnection.disconnect();
    }
    catch (MalformedURLException e)
    {
        toastText("test1");
        e.printStackTrace();
        TextView tv = (TextView) findViewById(R.id.textView3);
        tv.setText(e.getMessage());
    }
    catch (IOException e)
    {
        toastText("test2");
        e.printStackTrace();
        TextView tv = (TextView) findViewById(R.id.textView3);
        tv.setText(e.getMessage());
    }
    finally
    {

    }
}

private void readStream(InputStream in)
{
    try
    {
        toastText("test3");
        BufferedReader r = new BufferedReader(new InputStreamReader(in));
        StringBuilder total = new StringBuilder();
        String line;
        while ((line = r.readLine()) != null)
        {
            total.append(line);
        }

        TextView tv = (TextView) findViewById(R.id.textView3);
        tv.setText(total.toString());
    }
    catch (IOException e)
    {
        toastText("test4");
        e.printStackTrace();
        TextView tv = (TextView) findViewById(R.id.textView3);
        tv.setText(e.getMessage());
    }
}
수잔 (120 포인트) 님이 2016년 4월 23일 질문

1개의 답변

0 추천
StringBuilder sb = new StringBuilder(4096);
        try {
            URL url = new URL(strUrl);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.connect();

            BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            String temp = "";
            while((temp = br.readLine()) != null){
                sb.append(temp);
            }    

 

이런식으로 바궈보셈
익명사용자 님이 2016년 5월 12일 답변
...