昨天的问题
手机无法通过网络用socket连接服务器
改为使用其他的端口,设置本地计算机为服务器均无法解决问题,在设置本地计算机为服务器时还出现了百度地图api连接异常的问题,显示AK鉴权失败,最终采用http协议代替套接字
通过http协议连接客户端与服务器端
Get
客户端
public void run() {
HttpPost httpRequest = new HttpPost(http://172.16.99.207:8080/AndroidServer/AndroidServerServlet);
List<namevaluepair> params = new ArrayList<namevaluepair>();
params.add(new BasicNameValuePair(clientData, 您好服务器端!));
try {
Message message = new Message();
Bundle bundle = new Bundle();
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));//设置请求参数项
HttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(httpRequest);//执行请求返回响应
if(httpResponse.getStatusLine().getStatusCode() == 200){//判断是否请求成功
bundle.putString(msg, EntityUtils.toString(httpResponse.getEntity()));
}else{
bundle.putString(msg, 没有获取到Android服务器端的响应!);
}
message.setData(bundle);
handler.sendMessage(message);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
服务器端
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
response.setContentType(text/plain; charset=UTF-8);
request.setCharacterEncoding(UTF-8);
System.err.println(request.getParameter(clientData));
PrintWriter printWriter = response.getWriter();
;
printWriter.flush();
printWriter.close();
}
}