自从最近几天试图得到这个以来,我处于沟渠中,但是无法取得任何预期的结果。我会尽力让自己明白我在寻找以下几点:
HttpClient client=new DefaultHttpClient();
HttpGet request=new HttpGet();
URI address=new URI("http://xxx.xxx.xxx.xxx:8080/MyServlet");
request.setUri(address);
HttpResponse response=client.execute(request);
请求响应和servlet的android设备的代码如上所示,但是当我在android设备中的response.toString()上调用toString方法时,它会产生一个带有一些数字序列的字符串,这对我来说是没有用的。我。
救命!救命! HELP!一个简单的例子可以帮助我,
答案 0 :(得分:3)
您可以使用不使用任何编码技术生成纯文本结果的servlet。
在服务器端,只需将doGet函数替换为:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("Hello World");
}
在客户端,您可以使用以下代码:
try {
final HttpClient httpClient = new DefaultHttpClient();
final HttpGet httpGet = new HttpGet("http://SERVLET_URL/");
HttpResponse response = httpClient.execute(httpGet);
final HttpEntity entity = response.getEntity();
Log.i(TAG, "Servlet Result: " + EntityUtils.toString(entity));
} catch (ClientProtocolException e) {
Log.e(TAG, "ClientProtocolException", e);
} catch (ParseException e) {
Log.e(TAG, "ParseException", e);
} catch (IOException e) {
Log.e(TAG, "IOException", e);
}