2011-11-26 2 views
2

이미지 파일을 클라이언트로 전송하는 웹 서비스를 만들고 있습니다. 클라이언트의 경우 Apache HttpClient API를 사용합니다. 안드로이드에서 코드를 작성하기 전에 서비스를 테스트하고 있기 때문입니다. 당신이 반 (정도) 픽셀이 전송에서 길을 잃었지만, 나는 그것 때문에 약간의 전송 도둑의인지 모르거나 내가 대답을 잘못 해석하고 있습니다 볼 수 있듯이이상한 JAX-RS 동작

Image Sent Image Recieved

(아마 후자). 구문 분석을 위해 SAX를 사용하고 있는데, InputStream을 XML 형식으로받습니다.

XML 형식은 다음과 같이이다 :

여기
private static void download() { 
     try { 
      System.out.println("Downloading file..."); 

      HttpClient client = new DefaultHttpClient(); 
      HttpGet request = new HttpGet(SERVICE_URL + "download/me"); 

      HttpResponse response = client.execute(request); 

      if (response.getStatusLine().getStatusCode() != 200) { 
       System.out.println("Error: HTTP error code: " 
         + response.getStatusLine().getStatusCode()); 
      } 

      InputStream xml = response.getEntity().getContent(); 
      Image i = parseDownload(xml); 
      System.out.println(i); 

      client.getConnectionManager().shutdown(); 

      FileOutputStream fos = new FileOutputStream(
        new File("D:/newMe.jpg")); 
      fos.write(i.getData()); 
      fos.close(); 

     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

내 파싱 코드 : 여기

<Image> 
    <data>some Base64 encoded bytes</data> 
    <name>image name</name> 
</Image> 

웹 서비스 클라이언트 코드의

private static Image parseDownload(InputStream xml) { 
     final Image i = new Image(); 
     try { 
      SAXParserFactory spf = SAXParserFactory.newInstance(); 
      SAXParser parser = spf.newSAXParser(); 

      DefaultHandler handler = new DefaultHandler() { 
       private boolean name = false; 
       private boolean data = false; 

       @Override 
       public void startElement(String uri, String localName, 
         String qName, Attributes attributes) 
         throws SAXException { 
        if (qName.equals("name")) 
         name = true; 
        if (qName.equals("data")) 
         data = true; 
       } 

       @Override 
       public void characters(char[] ch, int start, int length) 
         throws SAXException { 
        if (name) { 
         i.setName(new String(ch, start, length)); 
         name = false; 
        } 
        if (data) { 
         String data = new String(ch, start, length); 
         i.setData(Base64.decodeBase64(data)); 
         this.data = false; 
        } 
       } 
      }; 

      Reader reader = new InputStreamReader(xml); 

      InputSource is = new InputSource(reader); 
      is.setEncoding("UTF-8"); 

      parser.parse(is, handler); 
     } catch (ParserConfigurationException e) { 
      e.printStackTrace(); 
     } catch (SAXException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return i; 
    } 

어떤 생각이 무엇을 내가 잘못하고있어?

답변

3

예,이 사이트와 다른 많은 사람들이 대답 시간과 시간을 다시 볼 수 있습니다. 메서드 호출은 1 메서드 호출에서 요소의 모든 콘텐츠를 제공하지 않을 수 있습니다. 모든 문자 메서드 호출을 하나의 버퍼로 집계하고 endElement() 메서드 호출을받을 때만 처리해야합니다.

+0

감사합니다. –