我正在构建一个Android应用程序,我通过Web服务连接到数据库。当我通过Web服务发送信息时,我得到了一个xml结果,然后我将其转换为字符串并将其显示在edittext中就像这样
Handler uiThreadHandler = new Handler() {
public void handleMessage(Message msg) {
Object o = msg.obj;
EditText textIn = (EditText)((Activity)context).findViewById(R.id.editTextDoSelectResult);
textIn.setText(o.toString());
System.out.println(o.toString());
}
};
Message msg = uiThreadHandler.obtainMessage();
msg.obj = result;
msg.arg1 = sid;
uiThreadHandler.sendMessage(msg);
}
我试图解析XML以便从xml结果中获取特定值并将其显示在相应的字段中,我想我应该遵循这样的事情
http://developer.android.com/training/basics/network-ops/xml.html
我的问题是,如何在我的代码中使用xml结果作为XmlPullParser中的输入流
如果还有,我可以改进这个问题,请告诉我
答案 0 :(得分:0)
也许它会帮助你。
获取输入流:
private static InputStream downloadUrl(String urlString) throws IOException {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
return conn.getInputStream();
}
将输入流转换为字符串:
private static String convertStreamToString(InputStream is) {
ByteArrayOutputStream oas = new ByteArrayOutputStream();
copyStream(is, oas);
String t = oas.toString();
try {
oas.close();
oas = null;
} catch (IOException e) {
e.printStackTrace();
}
return t;
}
设置为edittext:
...
youredittext.setText(convertStreamToString(downloadUrl(your_url)));
...