我正在使用以下方法提取将XML转换为JSON。
效果很好,但有时会给我ANR对话..有什么帮助吗?
public static void getResponse(final String xmlLink, final Activity activity,
final GetJSONRespone getjson) {
Handler uiHandler = new Handler(Looper.getMainLooper());
uiHandler.post(new Runnable() {
@Override
public void run() {
URL url = null;
BufferedReader in = null;
try {
url = new URL(xmlLink);
in = new BufferedReader(
new InputStreamReader(
url.openStream(), "UTF-8"));
String inputLine;
System.setProperty("http.keepAlive", "false");
StringBuilder builder = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
builder.append(inputLine);
}
String urlContent = builder.toString();
XmlToJson xmlToJson = new XmlToJson.Builder(urlContent).build();
JSONObject response = xmlToJson.toJson();
Log.i("response: ", response.toString());
getjson.getJSON(response);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
});
}
答案 0 :(得分:1)
当应用程序的主线程被阻塞太长时,会出现此对话框。因此,请尝试使用ASYNCTASK来避免此停止。