我正在使用以下代码使用 get 方法将一些信息发送到笔记本电脑上 xampp 服务器上运行的 servlet,AVD 也在同一台笔记本电脑上运行。我可以使用 AVD 中的浏览器访问 servlet,但可以从我创建的活动中访问。
public void get() throws Exception{
new Thread() {
@Override
public void run() {
TextView tv=(TextView) findViewById(R.id.statusView);
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
try{
URI address=new URI("http://10.0.2.2:8080/HelloWorld?FirstName=saligrma&LastName=verma");
request.setURI(address);
tv.setText("URI build \n");
}
catch(Exception e)
{
//koi bat ni na;
tv.setText("Not able to build the URI \n");
}
// setProxyIfNecessay(context, request);
try {
HttpResponse response = client.execute(request);
// tv.setText("StatusCode: " + response.getStatusLine().getStatusCode() );
} catch (Exception e) {
tv.setText("some error occured ");
}
}
}.start();
}
我的 servlet 代码在这里
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws IOException
{ java.sql.Connection con = null;
String url = "jdbc:mysql://localhost:3306/myDataBase";
String user="root";
String password="root";
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String fn= request.getParameter("FirstName");
String ln= request.getParameter("LastName");
try {Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(url, user, password);
Statement st = (Statement) con.createStatement();
st.executeUpdate("INSERT INTO firstName VALUES('"+fn+"')");
con.close();
}
catch (Exception ex) {
out.println("<p> Error Error Occured </p>");
}
}