AsyncTaskを使用してJSONをWebから取得し、doInBackground()メソッドでJSONを解析します。 onPreExecute()でprogressDialogを開始し、onPostExecute()でそれを終了します。問題は、私のダイアログが決して却下されない、そして、私は待っている/強制的に近い質問を受けるか、エミュレータがちょうどフリーズするということです。私はローカルファイルからJSONを解析してみました。 doInBackground()の2番目のlog.d()は決して印刷されず、最初のものだけが印刷されます。私はmistekesをしていますAsyncTaskを使用してJSONを取得するときにProgressDialogが決して解除されない
private class DownloadFilesTask extends AsyncTask<String, Void, String> {
protected String doInBackground(String... url) {
DefaultHttpClient httpClient = new DefaultHttpClient();
URI uri;
InputStream data=null;
String x="";
try {
uri = new URI(url[0]); //url of my JSON file
HttpGet method = new HttpGet(uri);
HttpResponse response = httpClient.execute(method);
data = response.getEntity().getContent();
byte [] buffer = new byte[data.available()];
Log.d("First: ","ziv");
while (data.read(buffer) != -1);
Log.d("Second: ",Integer.toString(buffer.length));
String jsontext = new String(buffer);
JSONArray entries = new JSONArray(jsontext);
x = "JSON parsed.\nThere are [" + entries.length() + "]\n\n";
int i;
for (i=0;i<entries.length();i++)
{
JSONObject post = entries.getJSONObject(i);
x += "------------\n";
x += "Date:" + post.getString("created_at") + "\n";
x += "Post:" + post.getString("text") + "\n\n";
}
}
catch (IOException je)
{
je.printStackTrace();
}
catch (URISyntaxException ur) {
ur.printStackTrace();
}
catch (JSONException je) {
tvData.setText("Error w/file: " + je.getMessage());
}
return x;
}
:
私のdoInBackground()は次のようになりますか?
Tnx、私はすべてを複雑にしていました。 – DixieFlatline