昨天的问题
手机无法通过网络用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();
}
}
客户端出现了jar包无法下载的问题,用到的方法也就没有了来源
现采用其他的方法
首先是客户端
使用httpclient
Get方法方便调试,暂时使用Get方法
new Thread(){
public void run(){
try {
//创建一个浏览器
HttpClient client=new DefaultHttpClient();
//声明一个Get 请求
String urlPath ="http://123.57.56.94:8080/server/servlet?"+"latitude="+testString;
HttpGet request =new HttpGet(urlPath);
//浏览器执行Get请求:client.execute()
HttpResponse response=client.execute(request);
//返回结果 验证响应码
int responseCode = response.getStatusLine().getStatusCode();
if(responseCode==200){
//返回了数据实体
HttpEntity entity=response.getEntity();
InputStream is=entity.getContent();//字节流接收
BufferedReader reader=new BufferedReader(new InputStreamReader((is)));//创建字节流的reader
final String line=reader.readLine();//reader 将字节流转为字符串
Log.v("response data",line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
};
服务器端
首先在本地做一个简单的服务器发布到Tomcat
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class GetPosition
*/
@WebServlet(description = "接收来自客户端的位置信息", urlPatterns = { "/GetPosition" })
public class GetPosition extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public GetPosition() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
response.setContentType("charset=utf-8");
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
response.setContentType("charset=utf-8");
String latitude=request.getParameter("latitude");
System.out.print(latitude);
//response.sendRedirect("../index.jsp");
}
}
本地服务器配置完成,将其部署到服务器 修改localhost为服务器的ip后出现了问题
响应时间过长
可能的原因
- 网站服务器故障维修(暂且排除)
- 网络连接不通(排除)
- 网站地址本身无法访问(重点检查)
- 网站程序问题(在本地已经可以运行且无法访问服务器上的其他网页)
发现问题:
在服务器上使用ip代替localhost也不能访问服务器上的内容-->ip地址的问题
服务器的公网和私网ip地址区别问题,使用公网地址无法在服务器内部访问,私网地址才可访问,但客户端无论使用私网地址还是公网地址都不能访问