我没有收到任何错误,但是当我尝试从 json 获取数据并以列表形式查看它时,进度条一直在运行。尽管当我尝试以 TextView 形式显示数据时它成功运行。
代码:主类
private static String url = "http://api.androidhive.info/contacts/";
private static final String TAG_CONTACTS = "contacts";;
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";
private static final String TAG_GENDER = "gender";
JSONArray contacts = null;
ArrayList <HashMap <String,String>> contactList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_of_occupation);
contactList = new ArrayList <HashMap <String,String>>();
ListView lv= getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
String name = ((TextView) view.findViewById(R.id.identity)).getText().toString();
Intent in = new Intent(getApplicationContext(), SingleContactActivity.class);
in.putExtra(TAG_NAME, name);
startActivity(in);
}
});
Button b1= (Button) findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
new ProgressTask().execute();
}
});
}
public class ProgressTask extends AsyncTask<Void, Void, Void> {
private ProgressDialog Dialog= new ProgressDialog(ListOfOccupation.this);
private Context context;
protected void onPreExecute() {
Dialog.setMessage("Please wait..");
Dialog.show();
}
protected void onPostExecute() {
if (Dialog.isShowing()) {
Dialog.dismiss();
}
ListAdapter adapter = new SimpleAdapter(context, contactList, R.layout.list_item,
new String[] {TAG_ID }, new int[] {R.id.identity});
setListAdapter (adapter);
}
protected Void doInBackground(Void... params) {
ServiceHandler sh = new ServiceHandler();
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
if (jsonStr !=null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
contacts = jsonObj.getJSONArray(TAG_CONTACTS);
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String email = c.getString(TAG_EMAIL);
String gender = c.getString(TAG_GENDER);
HashMap<String, String> contact = new HashMap<String, String>();
contact.put(TAG_ID, id);
contact.put(TAG_NAME, name);
contact.put(TAG_EMAIL, email);
contact.put(TAG_GENDER, gender);
contactList.add(contact);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// TODO Auto-generated method stub
return null;
}
}
代码:servicehandler.java
public class ServiceHandler {
public static final int GET = 0;
static String response = null;
public ServiceHandler() {
}
public String makeServiceCall(String url, int method) {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpget);
HttpEntity entity = httpResponse.getEntity();
response = EntityUtils.toString(entity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
置空之后:
03-31 18:30:53.406: E/AndroidRuntime(1159): FATAL EXCEPTION: main
03-31 18:30:53.406: E/AndroidRuntime(1159): java.lang.NullPointerException
03-31 18:30:53.406: E/AndroidRuntime(1159): at android.widget.SimpleAdapter.<init>(SimpleAdapter.java:85)
03-31 18:30:53.406: E/AndroidRuntime(1159): at com.example.listofoccupation.ListOfOccupation$ProgressTask.onPostExecute(ListOfOccupation.java:77)
03-31 18:30:53.406: E/AndroidRuntime(1159): at com.example.listofoccupation.ListOfOccupation$ProgressTask.onPostExecute(ListOfOccupation.java:1)
03-31 18:30:53.406: E/AndroidRuntime(1159): at android.os.AsyncTask.finish(AsyncTask.java:631)
03-31 18:30:53.406: E/AndroidRuntime(1159): at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-31 18:30:53.406: E/AndroidRuntime(1159): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
03-31 18:30:53.406: E/AndroidRuntime(1159): at android.os.Handler.dispatchMessage(Handler.java:99)
03-31 18:30:53.406: E/AndroidRuntime(1159): at android.os.Looper.loop(Looper.java:137)
03-31 18:30:53.406: E/AndroidRuntime(1159): at android.app.ActivityThread.main(ActivityThread.java:4745)
03-31 18:30:53.406: E/AndroidRuntime(1159): at java.lang.reflect.Method.invokeNative(Native Method)
03-31 18:30:53.406: E/AndroidRuntime(1159): at java.lang.reflect.Method.invoke(Method.java:511)
03-31 18:30:53.406: E/AndroidRuntime(1159): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-31 18:30:53.406: E/AndroidRuntime(1159): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-31 18:30:53.406: E/AndroidRuntime(1159): at dalvik.system.NativeStart.main(Native Method)
最佳答案
你的签名 onPostExecute
是错误的。在你的情况下它必须是
protected void onPostExecute(Void v)
如果您有 @Override
注释,您会立即意识到这一点。
编辑,您没有初始化上下文成员。改变
ListAdapter adapter = new SimpleAdapter(context, contactList, R.layout.list_item,
new String[] {TAG_ID }, new int[] {R.id.identity});
与
ListAdapter adapter = new SimpleAdapter(ListOfOccupation.this, contactList, R.layout.list_item,
new String[] {TAG_ID }, new int[] {R.id.identity});
关于java - 从 JSON 中获取数据不会以列表形式返回数据。进度条一直在跑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22761919/