我想在Android應用程序本地運行的Tomcat中調用一個servlet。我不確定它是否到達了servlet,因爲我沒有看到來自servlet的任何系統打印輸出。當我點擊瀏覽器上的網址時,我看到了一些東西,但不是來自android應用。android-使用apache HttpClient
我有這個在我的AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
這裏是我的代碼。
HttpClient client = new DefaultHttpClient();
HttpPost method = new HttpPost("http://192.168.1.6:8080/sampleweb/request");
List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>();
nameValuePairs.add(new BasicNameValuePair("req", xml));
method.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = (BasicHttpResponse) client.execute(method);
InputStream ips = response.getEntity().getContent();
BufferedReader buf = new BufferedReader(new InputStreamReader(ips, "UTF-8"));
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
throw new Exception(response.getStatusLine().getReasonPhrase());
}
StringBuilder sb = new StringBuilder();
String s;
while (true) {
s = buf.readLine();
if (s == null || s.length() == 0)
break;
sb.append(s);
}
buf.close();
ips.close();
System.out.println("response="+sb.toString());
client.getConnectionManager().shutdown();
輸出response.getStatusLine()。getStatusCode()來logcat中,看到你的GOT,它會告訴你關於你的HttpResponse的基本信息。 – yorkw 2012-02-28 08:23:07