我正在使用 JSP 开发网络应用程序。对于同一个项目,我正在开发一个 Android 应用程序。 Web 应用程序使用 Apache Tomcat 和 MySQL。现在我想通过从 MySQL 数据库中检索数据从 Android 应用程序登录。但是如何呢?
我确实找到了很多教程,但都是使用 PHP 脚本。我正在为这两个应用程序使用 Eclipse。
最佳答案
Android 试试这个。
private static HttpClient getHttpClient() {
if (mHttpClient == null) {
mHttpClient = new DefaultHttpClient();
final HttpParams params = mHttpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
}
return mHttpClient;
}
然后
public static String sendFirst(String requestString) throws Exception {
BufferedReader in = null;
try {
HttpClient client = getHttpClient();
HttpPost request = new HttpPost(universal_URL_MENU+"?request_menu="+start_menu);
HttpResponse response = client.execute(request);
System.out.println("response in class"+response);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
result = sb.toString();
// }
}catch(Exception e){
e.printStackTrace();
System.out.println("catch");
}
finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
在哪里
public static String universal_URL_MENU = "http://192.***.1.@:9999/my_Project/ReqFromTabFor.do";
现在用于 Jsp 或 Servlet
try{
PrintWriter out=res.getWriter();
String subcategory=req.getParameter("request_menu");
System.out.println("Receive : "+subcategory);
JSONObject jobj=UserDelegate.reqFromTabForMenuBySCatg(subcategory);
}
if(jobj!=null){
out.println(jobj);
}else{
out.print("Sorry Not Available");
}
}catch(Exception e){ e.printStackTrace(); }
关于java - 使用 Jsp 与 Web 应用程序的 Android 应用程序数据库连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22285223/