XMLをJSONに変換するために以下の方法を使用しています。Application Not Respondingダイアログが表示されないようにする方法
- まず、xmlのapi呼び出しのURLを渡してから、 のデータを抽出してStringに入れます。
- このLibraryを使用すると、I はxmlをJSONオブジェクトに変換します。
- 最後に、私はjsonオブジェクトデータ を使用してWebデータを取得し、私のrecyclerviewsを設定します。
それはうまく動作しますが、時にはそれは私に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();
}
}
}
}
});
}
APIからデータをダウンロードするにはasyntaskを使用するか、vollyやRetrofitのようなサードパーティライブラリを使用します。 –
https://developer.android.com/training/articles/perf-anr.html#anr – Kuffs
VolleyまたはRetroFitを使用してJSONレスポンスを試してください。ANRを処理するには、この-https://developer.android.comをご覧ください。 /training/articles/perf-anr.html –