Das ist mein AsyncTaskAsyncTask Fehler onPostExexcute wird nie
private class RetrieveFeedTask extends AsyncTask<Void, Void, String> {
private Exception exception;
protected void onPreExecute() {
progressBar.setVisibility(View.VISIBLE);
sqNameTV.setText("");
sqdescriptionTV.setText("");
sqdetailedDescriptionTV.setText("");
}
protected String doInBackground(Void... urls) {
try {
URL url = new URL(URLi);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line).append(" ");
}
bufferedReader.close();
String ress = stringBuilder.toString();
JSONObject object = new JSONObject(ress);
JSONArray itemListElement = object.getJSONArray("itemListElement");
InputStream is;
for (int i = 0; i < itemListElement.length(); i++) {
JSONObject c = itemListElement.getJSONObject(i);
JSONObject results = c.getJSONObject("result");
JSONObject image = results.getJSONObject("image");
String contentUrl = image.getString("contentUrl");
is = (InputStream) new URL(contentUrl).getContent();
d = Drawable.createFromStream(is, "src name");
}
return stringBuilder.toString();
} finally {
urlConnection.disconnect();
}
} catch (Exception e) {
Log.e("ERROR", e.getMessage(), e);
return null;
}
}
protected void onPostExecute(String response, Drawable dd) {
if (response == null) {
response = "THERE WAS AN ERROR";
Toast.makeText(MainActivity.this, "post execute", Toast.LENGTH_LONG).show();
}
progressBar.setVisibility(View.GONE);
Log.i("INFO", response);
// Appropriate error handling code
try {
JSONObject object = new JSONObject(response);
JSONArray itemListElement = object.getJSONArray("itemListElement");
for (int i = 0; i < itemListElement.length(); i++) {
JSONObject c = itemListElement.getJSONObject(i);
JSONObject results = c.getJSONObject("result");
String name = results.getString("name").toString();
String description = results.getString("description").toString();
JSONObject image = results.getJSONObject("image");
String contentUrl = image.getString("contentUrl");
JSONObject detailedDescription = results.getJSONObject("detailedDescription");
String articleBody = detailedDescription.getString("articleBody");
sqNameTV.setText(name);
sqdescriptionTV.setText(description);
Drawable imageQ = LoadImageFromWebOperations(contentUrl);
Log.e("TAG",contentUrl);
sIV.setImageDrawable(d);
sqdetailedDescriptionTV.setText(articleBody);
}
} catch (JSONException e) {
sqNameTV.setText("Empty");
sqdescriptionTV.setText("No Data Found");
sqdetailedDescriptionTV.setText("Could not find any data on " + queryyy);
}
}
}
private Drawable LoadImageFromWebOperations(String strPhotoUrl) {
try {
InputStream is = (InputStream) new URL(strPhotoUrl).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
Log.e("TAGG", strPhotoUrl);
return d;
} catch (Exception e) {
Log.e("TAGG", e.toString());
return null;
}
und es war richtig funktionieren genannt. Später, da ich auch ein Bild bekommen musste, habe ich ein paar JSON-Parsing in doInBackground() eingefügt. Dann wird onPostExecute aus irgendeinem Grund nie aufgerufen. Irgendeine Idee warum? Danke
haben Sie Test ist, dass Sie ein Timeout oder haben Ausnahme? – ARP
Nein. Mein Fortschrittsbalken dreht sich gerade weiter. Kein Protokoll, dass onPostExecute gestartet –