If you think the Android project Locast-Core-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package edu.mit.mobile.android.json;
/*//www.java2s.com
* Copyright (C) 2011 MIT Mobile Experience Lab
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/import java.io.IOException;
import java.net.URI;
import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.BaseAdapter;
/**
* An adapter for either a static JSONArray or a remote list that is loaded from a network.
* If you need to do custom network requests, override getHttpClient() and getHttpGet().
* You don't need to call them.
*
* @author steve
*
*/publicabstractclass JSONArrayAdapter extends BaseAdapter {
private JSONArray ar;
private LoadJSONArrayTask loadTask;
public JSONArrayAdapter(Context context, JSONArray jsonArray) {
ar = jsonArray;
}
public JSONArrayAdapter(Context context, URI remoteList){
ar = new JSONArray(); // temporary until we get our content...
loadTask = new LoadJSONArrayTask();
loadTask.execute(remoteList);
}
publicint getCount() {
return ar.length();
}
publicvoid setJSONArray(JSONArray jsonArray){
ar = jsonArray;
notifyDataSetChanged();
}
public Object getItem(int position) {
try {
return ar.get(position);
} catch (final JSONException e) {
e.printStackTrace();
return null;
}
}
publiclong getItemId(int position) {
return position;
}
public HttpClient getHttpClient(){
returnnew DefaultHttpClient();
}
public HttpGet getHttpGet(URI uri){
returnnew HttpGet(uri);
}
publicvoid onPreExecute(){
}
publicvoid onPostExecute(JSONArray result){
}
privateclass LoadJSONArrayTask extends AsyncTask<URI, Double, JSONArray>{
@Override
protectedvoid onPreExecute() {
JSONArrayAdapter.this.onPreExecute();
}
@Override
protected JSONArray doInBackground(URI... params) {
JSONArray result = null;
final HttpClient hc = getHttpClient();
final HttpGet req = getHttpGet(params[0]);
try {
result = new JSONArray(hc.execute(req, new BasicResponseHandler()));
} catch (final HttpResponseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (final IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (final JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
@Override
protectedvoid onPostExecute(JSONArray result) {
JSONArrayAdapter.this.onPostExecute(result);
if (result == null){
return;
}
setJSONArray(result);
}
}
}