- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
package com.example.hstnc_activity;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.JsonReader;
import android.view.MenuItem;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class DisplayServiceActivity extends ListActivity {
private ListView listOfServices;
//JSONArrays?
JSONArray directory = null;
//JSON Node names
private static String TAG_ID = "id";
private static String TAG_NAME= "name";
private static String TAG_DIRECTORY = "Categories";
private final static String url;
JSONObject json;
jsonParser jParser = new jsonParser();
@SuppressLint("NewApi")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new Request().onPostExecute(url);
listOfServices =getListView(); //get builtin listView
ArrayList<HashMap<String, String>> directoryList = new ArrayList<HashMap<String, String>>();
// Intent intent = getIntent();
//String url = intent.getStringExtra("SERVICES_DIRECTORY");
try{
//getting Array
directory = json.getJSONArray(TAG_DIRECTORY);
for(int i= 0; i<directory.length(); i++){
JSONObject addItem =directory.getJSONObject(i);
//store each item in variable
String id = addItem.getString(TAG_ID);
String name= addItem.getString(TAG_NAME);
//create new HashMap
HashMap<String,String> map = new HashMap<String, String>();
//add each child node to HashMap key
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
//adding HashList to ArrarList
directoryList.add(map);
}
} catch (JSONException e){
e.printStackTrace();
}
ListAdapter adapter = new SimpleAdapter(this,
directoryList,
R.layout.list_item,
new String[] { TAG_ID,TAG_NAME },
new int[] { android.R.id.text1,android.R.id.text2 });
setListAdapter(adapter);
setContentView(R.layout.service);
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}// end of onCreate Method
@SuppressWarnings("unused")
public class Request extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... params) {
json = jParser.getJSONfromURL(url);
return null;
}
protected void onPostExecute(String url) {
json = jParser.getJSONfromURL(url);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
我正在尝试在 Android 中执行 JSON 请求并获取信息并将其放入 ListView 中。当我调试时,我不断收到“HTTP 连接错误 android.os.NetworkOnMainThreadException”错误。运行 apk 时,当我打开此 Activity 时,它只是强制关闭。此 Activity 是通过另一个屏幕上的按钮启动的。
最佳答案
为什么这个 json = jParser.getJSONfromURL(url)
出现在 onPostExecute()
中?我假设您只是忘记删除它,因为您在 doInBackground()
中也有它应该在的位置
将其移出那里,因为 onPostExecute()
在 UI 线程
上运行,您将收到 NetworkOnMainThreadException
我没有注意到 UI
上运行的任何网络操作,但如果有,则将其移至 doInBackground()
此外,您不应直接调用 onPostExecute()
或任何其他 AsyncTask
方法。你需要用类似的东西来做到这一点
Request = new Request(); // can send parameters to constructor if needed
request.execute(); // execute doInBackground()-- you can pass your url param in here for doInBackground to receive but you have to change the class declaration so that the first param takes a url
关于java - json 请求的 AsycTask 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16503743/
我看过很多关于 AsycTask 未运行问题的帖子,想确认这是否确实是我的实现问题。 Should I try to implement this method instead? 我使用 new Ge
package com.example.hstnc_activity; import java.io.InputStream; import java.io.InputStreamReader; im
我正在使用 AsyncTask 从服务器中检索一些显示在屏幕上的数据。问题是如果我在检索数据时更改设备的方向(显示进度条),我的 UI 不会更新。 我已经做了以下处理来处理这种情况: a) 覆盖返回我
你好,我目前无法确定在提取 url 后如何处理图像,因为异步只允许返回一次,有人有什么建议吗? /** * After completing background task Dism
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: android - Caused by: android.view.ViewRootImpl$CalledF
我的 Android 应用程序中有以下 AsyncTask。此 AsyncTask 包含在扩展 PreferenceFragment 的类的 OnCreate() 方法中。 public class
当我使用应用程序时,我开始知道在 AsycTask 中我不能直接使用 Toast,除非它是一个处理程序。所以对于处理程序,到目前为止它工作正常。 Toast 作为处理程序如下 private void
我是一名优秀的程序员,十分优秀!