public void consumeIt(){
HttpConnection con = null;
InputStream is = null;
try {
String url = "http://where.yahooapis.com/geocode?q=1600+Pennsylvania+Avenue,+Washington,+DC";
System.out.println("poo" + url);
con = (HttpConnection) Connector.open(url);
final int responseCode = con.getResponseCode();
if (responseCode != HttpConnection.HTTP_OK) {
System.out.println("response code:" + responseCode);
}
System.out.println("Works here");
is = con.openInputStream();
byte[] responseData = new byte[10000];
int length = 0;
StringBuffer rawResponse = new StringBuffer();
while (-1 != (length = is.read(responseData))) {
rawResponse.append(new String(responseData, 0, length));
}
final String result = rawResponse.toString();
System.out.println("result:" + result);
}
catch (final Exception ex) {
System.out.println("error:" + ex.getMessage());
}
finally {
try {
is.close();
is = null;
con.close();
con = null;
}
catch(Exception e){}
}
}
我發現上面的代碼應該抓住api返回的xml數據,但我似乎無法得到它的工作。用「poo」打印的系統顯示出來,但不顯示「Works here」。我不知道什麼是錯的。我在9700模擬器中運行它。黑莓:消耗一個寧靜的網絡服務
爲什麼連接到Web服務如此複雜!
請嘗試下面的代碼,讓我知道它工作與否 –