2
이미지 파일을 클라이언트로 전송하는 웹 서비스를 만들고 있습니다. 클라이언트의 경우 Apache HttpClient
API를 사용합니다. 안드로이드에서 코드를 작성하기 전에 서비스를 테스트하고 있기 때문입니다. 당신이 반 (정도) 픽셀이 전송에서 길을 잃었지만, 나는 그것 때문에 약간의 전송 도둑의인지 모르거나 내가 대답을 잘못 해석하고 있습니다 볼 수 있듯이이상한 JAX-RS 동작
(아마 후자). 구문 분석을 위해 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;
}
어떤 생각이 무엇을 내가 잘못하고있어?
감사합니다. –