今天服务器返回的数据格式既有json又有xml;json好说,这里只需要解析出name标签对应的值
xml内容如下
annotation>
JPEGImages
000111.jpg
D:\\数据集\\上马店图像\\1101Add\\JPEGImages\\000111.jpg
代码
public void upLoad(View mView){
RequestParams params=new RequestParams("http://192.168.X.XXX:8080/XXX/XX");
// 有上传文件时使用multipart表单, 否则上传原始文件流.
params.setMultipart(true);
// 上传文件方式 2 /mnt/internal_sd/FtrendCrashLog/th.jpg
params.addBodyParameter("file", new File("/mnt/internal_sd/FtrendCrashLog/th.jpg"));
Callback.Cancelable cancelable
= x.http().post(params,new Callback.CommonCallback() {
@Override
public void onSuccess(JsonDemoResponse result) {
Recognize recognize= JSON.parseObject(result.toString(),Recognize.class);
String data= recognize.getData();
Log.e("wy","run: "+ data);
domParse(data);
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
//Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();
if (ex instanceof HttpException) { // 网络错误
HttpException httpEx = (HttpException) ex;
int responseCode = httpEx.getCode();
String responseMsg = httpEx.getMessage();
String errorResult = httpEx.getResult();
// ...
} else { // 其他错误
// ...
}
Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();
}
@Override
public void onCancelled(CancelledException cex) {
Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
}
@Override
public void onFinished() {
Log.e("wy","上传完成 ");
}
});
内部类
class SAXDemoHandel extends DefaultHandler {
private boolean isName=false;
//遍历xml文件开始标签
@Override
public void startDocument() throws SAXException {
super.startDocument();
System.out.println("sax解析开始");
}
//遍历xml文件结束标签
@Override
public void endDocument() throws SAXException {
super.endDocument();
System.out.println("sax解析结束");
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
if (qName.equals("name")){
System.out.println("============开始遍历name=============");
isName=true;
//System.out.println(attributes.getValue("rollno"));
// Log.e("wy","解析到商品编码: "+ attributes.getValue(qName));
}
// else if (!qName.equals("name")&&!qName.equals("class")){
// System.out.print("节点名称:"+qName+"----");
// }
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
super.endElement(uri, localName, qName);
if (qName.equals("name")){
System.out.println(qName+"遍历结束");
System.out.println("============结束遍历name=============");
isName=false;
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
super.characters(ch, start, length);
String value = new String(ch,start,length).trim();
if (!value.equals("")&& isName) {
// System.out.println(value);
Log.e("wy","解析到商品编码: "+ value);
}
}
}