android客户端怎么与服务器交互
1个回答
2016-08-13 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
1、java服务器建立至少一种服务webservices、servlet、socket
2、客户端通过socket或者httpurlconnection的方式进行连接访问
服务端:
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
resp.setContentType("text/html;charset=utf-8");
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
PrintWriter out = resp.getWriter();
//用HTML格式给浏览器返回数据
out.println("<html>");
out.println("<head>");
out.println("<title>Tomcat Servlet测试</title>");
out.println("</head>");
out.println("<body>");
out.println("Hello,First Servlet!");
out.println("</body>");
out.println("</html>");
out.println("Hello,第一个Tomcat!!!");
out.close();
}
客户端:
private String doGet(String url){
String responseStr = "";
try {
String name = nameEdit.getText().toString().trim();
String code = codeEdit.getText().toString().trim();
String getUrl = URL + "?NAME=" + name+"&"+"CODE=" + code;
HttpGet httpRequest = new HttpGet(getUrl);
HttpParams params = new BasicHttpParams();
ConnManagerParams.setTimeout(params, 1000);
HttpConnectionParams.setConnectionTimeout(params, 3000);
HttpConnectionParams.setSoTimeout(params, 5000);
httpRequest.setParams(params);
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
final int ret = httpResponse.getStatusLine().getStatusCode();
if(ret == HttpStatus.SC_OK){
responseStr = EntityUtils.toString(httpResponse.getEntity(), HTTP.UTF_8);
}else{
responseStr = "-1";
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return responseStr;
}
2、客户端通过socket或者httpurlconnection的方式进行连接访问
服务端:
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
resp.setContentType("text/html;charset=utf-8");
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
PrintWriter out = resp.getWriter();
//用HTML格式给浏览器返回数据
out.println("<html>");
out.println("<head>");
out.println("<title>Tomcat Servlet测试</title>");
out.println("</head>");
out.println("<body>");
out.println("Hello,First Servlet!");
out.println("</body>");
out.println("</html>");
out.println("Hello,第一个Tomcat!!!");
out.close();
}
客户端:
private String doGet(String url){
String responseStr = "";
try {
String name = nameEdit.getText().toString().trim();
String code = codeEdit.getText().toString().trim();
String getUrl = URL + "?NAME=" + name+"&"+"CODE=" + code;
HttpGet httpRequest = new HttpGet(getUrl);
HttpParams params = new BasicHttpParams();
ConnManagerParams.setTimeout(params, 1000);
HttpConnectionParams.setConnectionTimeout(params, 3000);
HttpConnectionParams.setSoTimeout(params, 5000);
httpRequest.setParams(params);
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
final int ret = httpResponse.getStatusLine().getStatusCode();
if(ret == HttpStatus.SC_OK){
responseStr = EntityUtils.toString(httpResponse.getEntity(), HTTP.UTF_8);
}else{
responseStr = "-1";
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return responseStr;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询