0
我有異步任務獲取json數組加載微調框。但在onPostExecute我得到了 NetworkOnMainThreadException錯誤。在AsyncTask onPostExecute得到了NetworkOnMainThreadException錯誤
//錯誤在這裏
while ((line = reader.readLine()) != null)
{
builder.append(line) ;
}
這裏是我的onPostExecute。響應是一個Http響應。這裏有什麼錯誤?
protected void onPostExecute(String result) {
BufferedReader reader;
try {
reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuilder builder = new StringBuilder();
String line = null;
//Error in here
while ((line = reader.readLine()) != null)
{
builder.append(line) ;
}
JSONTokener tokener = new JSONTokener(builder.toString());
JSONArray finalResult = new JSONArray(tokener);
final String[] items = new String[finalResult.length()];
// looping through All Contacts
for(int i = 0; i < finalResult.length(); i++){
JSONObject c = finalResult.getJSONObject(i);
items[i]=c.getString("data_name");
}
ArrayAdapter<String> adapter = new ArrayAdapter<String> (activity, android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
super.onPostExecute(result);
//activity.startActivity(new Intent(activity, wanted.class));
if (dialog != null)
dialog.dismiss();
}
唯一的東西我看需要在'onPostExecute()'中的是'Adapter'代碼和'Dialog'代碼。一切都應該在'doInBackground()' – codeMagic
不是**另一個**'NetworkOnMainThreadException'!請在您提問之前進行搜索。每週出現10次。 – Simon
問題已解決。 –