<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/lstText"
/>
</LinearLayout>listitems.xml
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/txtAlertText"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/txtAlertDate"/>
</LinearLayout>
</LinearLayout>package josecgomez.com.android.dev.webservice;
import java.util.List;
import josecgomez.com.android.dev.webservice.objects.alerts;
importandroid.content.Context;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.widget.ArrayAdapter;
importandroid.widget.LinearLayout;
import android.widget.TextView;public class AlertsAdapter extends ArrayAdapter<alerts> {
int resource;
Stringresponse;
Contextcontext;
//Initializeadapter
publicAlertsAdapter(Contextcontext,intresource,List<alerts>items){
super(context,resource,items);
this.resource=resource;}
@Override
publicViewgetView(intposition,ViewconvertView,ViewGroupparent)
{
LinearLayoutalertView;
//Getthecurrentalertobject
alerts al = getItem(position);//Inflate the view
if(convertView==null)
{
alertView=newLinearLayout(getContext());
Stringinflater=Context.LAYOUT_INFLATER_SERVICE;
LayoutInflatervi;
vi=(LayoutInflater)getContext().getSystemService(inflater);
vi.inflate(resource,alertView,true);
}
else
{
alertView=(LinearLayout)convertView;
}
//Getthetextboxesfromthelistitem.xmlfile
TextViewalertText=(TextView)alertView.findViewById(R.id.txtAlertText);
TextView alertDate =(TextView)alertView.findViewById(R.id.txtAlertDate);//Assign the appropriate data from our alert object above
alertText.setText(al.alerttext);
alertDate.setText(al.alertdate);return alertView; }
}
package josecgomez.com.android.dev.webservice;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import josecgomez.com.android.dev.webservice.objects.alerts;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class main extends Activity {
/** Called when the activity is first created. */
//ListView that will hold our items references back to main.xml
ListView lstTest;
//Array Adapter that will hold our ArrayList and display the items on the ListView
AlertsAdapter arrayAdapter;
//List that will host our items and allow us to modify that array adapter
ArrayList<alerts> alrts=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Initialize ListView
lstTest= (ListView)findViewById(R.id.lstText);
//Initialize our ArrayList
alrts = new ArrayList<alerts>();
//Initialize our array adapter notice how it references the listitems.xml layout
arrayAdapter = new AlertsAdapter(main.this, R.layout.listitems,alrts);
//Set the above adapter as the adapter of choice for our list
lstTest.setAdapter(arrayAdapter);
//Instantiate the Web Service Class with he URL of the web service not that you must pass
WebService webService = new WebService("http://www.sumasoftware.com/alerts/GetAlerts.php");
//Pass the parameters if needed , if not then pass dummy one as follows
Map<String, String> params = new HashMap<String, String>();
params.put("var", "");
//Get JSON response from server the "" are where the method name would normally go if needed example
// webService.webGet("getMoreAllerts", params);
String response = webService.webGet("", params);
try
{
//Parse Response into our object
Type collectionType = new TypeToken<ArrayList<alerts>>(){}.getType();
//JSON expects an list so can't use our ArrayList from the lstart
List<alerts> lst= new Gson().fromJson(response, collectionType);
//Now that we have that list lets add it to the ArrayList which will hold our items.
for(alerts l : lst)
{
alrts.add(l);
}
//Since we've modified the arrayList we now need to notify the adapter that
//its data has changed so that it updates the UI
arrayAdapter.notifyDataSetChanged();
}
catch(Exception e)
{
Log.d("Error: ", e.getMessage());
}
}
}Please note that the attached jar file contains a compiled copy of the GSON library available at http://code.google.com/p/google-gson/.
以上是转帖 http://www.josecgomez.com/2010/05/04/android-accessing-restfull-json-services-library/
http://www.josecgomez.com/2010/04/30/android-accessing-restfull-web-services-using-json/