“http://54.251.60.177/StudentWebService/StudentDetail.asmx/GetTMSOrders”
上面的链接包含XML格式的一些数据。现在我试图在android中的网格视图中显示这些值,但是我收到了一些错误,请问我在这里犯了什么错误!..
logcat的
09-03 10:30:07.226: D/AndroidRuntime(332): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
09-03 10:30:07.226: D/AndroidRuntime(332): CheckJNI is ON
09-03 10:30:07.356: D/AndroidRuntime(332): --- registering native functions ---
09-03 10:30:08.036: D/dalvikvm(178): GC_EXPLICIT freed 72 objects / 3544 bytes in 132ms
09-03 10:30:08.066: D/PackageParser(70): Scanning package: /data/app/vmdl26424.tmp
09-03 10:30:08.207: W/PackageManager(70): Attempt to re-install com.example.gridview without first uninstalling.
09-03 10:30:08.287: D/dalvikvm(70): GC_EXPLICIT freed 5642 objects / 319672 bytes in 82ms
09-03 10:30:08.336: D/AndroidRuntime(332): Shutting down VM
09-03 10:30:08.336: D/dalvikvm(332): Debugger has detached; object registry had 1 entries
09-03 10:30:08.356: I/AndroidRuntime(332): NOTE: attach of thread 'Binder Thread #3' failed
09-03 10:30:56.137: I/ActivityManager(70): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.android.music/.MusicBrowserActivity }
09-03 10:30:56.517: I/ARMAssembler(70): generated scanline__00000077:03545404_00000004_00000000 [ 47 ipp] (67 ins) at [0x2bbeb0:0x2bbfbc] in 1421135 ns
09-03 10:30:56.708: I/ActivityManager(70): Starting activity: Intent { act=android.intent.action.PICK dat= typ=vnd.android.cursor.dir/artistalbum flg=0x4000000 cmp=com.android.music/.ArtistAlbumBrowserActivity (has extras) }
09-03 10:30:57.067: I/AudioService(70): Remote Control registerMediaButtonEventReceiver() for ComponentInfo{com.android.music/com.android.music.MediaButtonIntentReceiver}
09-03 10:30:57.237: I/ActivityManager(70): Displayed activity com.android.music/.ArtistAlbumBrowserActivity: 512 ms (total 844 ms)
09-03 10:30:58.697: W/KeyCharacterMap(275): No keyboard for id 0
09-03 10:30:58.697: W/KeyCharacterMap(275): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
09-03 10:30:58.927: D/SntpClient(70): request time failed: java.net.SocketException: Address family not supported by protocol
09-03 10:31:00.417: I/AudioService(70): AudioFocus abandonAudioFocus() from android.media.AudioManager@44f1a920com.android.music.MediaPlaybackService$3@44f19a88
09-03 10:31:05.547: D/dalvikvm(231): GC_EXPLICIT freed 3657 objects / 221552 bytes in 193ms
09-03 10:31:10.567: D/dalvikvm(275): GC_EXPLICIT freed 2470 objects / 183448 bytes in 166ms
09-03 10:35:58.968: D/SntpClient(70): request time failed: java.net.SocketException: Address family not supported by protocol
09-03 10:40:58.997: D/SntpClient(70): request time failed: java.net.SocketException: Address family not supported by protocol
09-03 10:45:59.007: D/SntpClient(70): request time failed: java.net.SocketException: Address family not supported by protocol
09-03 10:49:41.567: D/dalvikvm(145): GC_FOR_MALLOC freed 7233 objects / 413488 bytes in 118ms
09-03 10:50:59.072: D/SntpClient(70): request time failed: java.net.SocketException: Address family not supported by protocol
09-03 10:55:59.127: D/SntpClient(70): request time failed: java.net.SocketException: Address family not supported by protocol
09-03 11:00:59.161: D/SntpClient(70): request time failed: java.net.SocketException: Address family not supported by protocol
09-03 11:05:59.197: D/SntpClient(70): request time failed: java.net.SocketException: Address family not supported by protocol
09-03 11:07:18.186: D/dalvikvm(70): GC_FOR_MALLOC freed 15725 objects / 719296 bytes in 81ms
09-03 11:10:59.200: D/SntpClient(70): request time failed: java.net.SocketException: Address family not supported by protocol
09-03 11:15:59.238: D/SntpClient(70): request time failed: java.net.SocketException: Address family not supported by protocol
09-03 11:20:59.257: D/SntpClient(70): request time failed: java.net.SocketException: Address family not supported by protocol
09-03 11:25:59.259: D/SntpClient(70): request time failed: java.net.SocketException: Address family not supported by protocol
09-03 11:30:59.288: D/SntpClient(70): request time failed: java.net.SocketException: Address family not supported by protocol
09-03 11:35:59.297: D/SntpClient(70): request time failed: java.net.SocketException: Address family not supported by protocol
GridView.java
public class GridView extends ListActivity
{
// All static variables
static final String URL = "http://54.251.60.177/StudentWebService/StudentDetail.asmx/GetTMSOrders";
// XML node keys
static final String KEY_TABLE = "Table";
static final String KEY_CUSTOMER = "Cust_Name";// parent node
static final String KEY_ORDER = "Order_No";
static final String KEY_FREIGHT = "Freight_Rate";
static final String KEY_STATION1 = "Station_Name";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_TABLE);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++)
{
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_CUSTOMER, parser.getValue(e, KEY_CUSTOMER));
map.put(KEY_ORDER,parser.getValue(e, KEY_ORDER));
map.put(KEY_FREIGHT, parser.getValue(e, KEY_FREIGHT));
map.put(KEY_STATION1, parser.getValue(e, KEY_STATION1));
// adding HashList to ArrayList
menuItems.add(map);
}
// Adding menuItems to ListView
SimpleAdapter adapter = new SimpleAdapter(this, menuItems,R.layout.grid_item,
new String[] { KEY_CUSTOMER, KEY_ORDER, KEY_FREIGHT ,KEY_STATION1}, new int[]
{ R.id.cust, R.id.order, R.id.freight ,R.id.station1});
setListAdapter(adapter);
// selecting single ListView item
GridView gv = getGridView();
gv.setContentView(new View.OnClickListener() {
@Override
public void onClick(AdapterView<?> parent View view,int position, long id)
{
// getting values from selected ListItem
String fieldtype = ((TextView) view.findViewById(R.id.cust)).getText().toString();
String fieldformat = (TextView)view.findViewById(R.id.order)).getText().toString();
String sample = ((TextView) view.findViewById(R.id.freight)).getText().toString();
String search = ((TextView)view.findViewById(R.id.station1)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
startActivity(in);
} }); } }
SingleMenuItemActivity.java
public class SingleMenuItemActivity extends Activity {
// XML node keys
static final String KEY_CUSTOMER = "Cust_Name";
static final String KEY_ORDER = "Order_No";
static final String KEY_FREIGHT = "Freight_Rate";
static final String KEY_STATION_1 = "Station_Name";
static final String KEY_STATION_2 = "Station_Name1";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_grid_item);
// getting intent data
Intent in = getIntent();
// Get XML values from previous intent
String cust = in.getStringExtra(KEY_CUSTOMER);
String order = in.getStringExtra(KEY_ORDER);
String freight = in.getStringExtra(KEY_FREIGHT);
String station1 = in.getStringExtra(KEY_STATION_1);
String station2 = in.getStringExtra(KEY_STATION_1);
// Displaying all values on the screen
TextView lblcust = (TextView) findViewById(R.id.cust_label);
TextView lblorder = (TextView) findViewById(R.id.order_label);
TextView lblfreight = (TextView) findViewById(R.id.freight_label);
TextView lblstation1 = (TextView) findViewById(R.id.station1_label);
TextView lblstation2 = (TextView) findViewById(R.id.station2_label);
lblcust.setText(cust);
lblorder.setText(order);
lblfreight.setText(freight);
lblstation1.setText(station1);
lblstation2.setText(station2);
}}
XMLPareser.java
public class XMLParser
{
// constructor
public XMLParser()
{
}
/**
* Getting XML from URL making HTTP request
* @param url string
* */
public String getXmlFromUrl(String url)
{
String xml = null;
try
{
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
// return XML
return xml;
}
/**
* Getting XML DOM element
* @param XML string
* */
public Document getDomElement(String xml)
{
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try
{
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
}
catch (ParserConfigurationException e)
{
Log.e("Error: ", e.getMessage());
return null;
}
catch (SAXException e)
{
Log.e("Error: ", e.getMessage());
return null;
}
catch (IOException e)
{
Log.e("Error: ", e.getMessage());
return null;
}
return doc;
}
/** Getting node value
* @param elem element
*/
public final String getElementValue( Node elem )
{
Node child;
if( elem != null)
{
if (elem.hasChildNodes())
{
for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() )
{
if( child.getNodeType() == Node.TEXT_NODE )
{
return child.getNodeValue();
}
}
}
}
return "";
}
/**
* Getting node value
* @param Element node
* @param key string
* */
public String getValue(Element item, String str)
{
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}}