I'm using the method below to extract to convert XML into JSON.!
- First I a pass the url for the api call of the xml and then extract the data and put it in String.
- Then using this Library, I convert the xml into JSON Object.
- Finally I use the json object data to get web data and populate my recyclerviews.
It works well, but sometimes it gives me ANR dialog..! any help?
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();
}
}
}
}
});
}