繁体   English   中英

使用Jsp的Android应用程序数据库与Web应用程序的连接

[英]Android application database connectivity with web app using Jsp

我正在用JSP开发一个Web应用程序。 对于同一项目,我正在开发一个Android应用程序。 该Web应用程序使用Apache Tomcat和MySQL。 现在,我想通过从MySQL数据库检索数据来从Android应用程序登录。 但是如何?

我确实找到了很多教程,但是都使用PHP脚本。 我将Eclipse用于这两个应用程序。

客户端 (您的Android应用)和服务器之间发生的事情是松散耦合的,这意味着它们之间没有任何关系,除了它们与之通信的协议(对于Web服务而言,协议是HTTP)。

通常,客户端(应用程序或Web浏览器)会发出HTTP请求,以POST或GET方法发送参数(例如登录名,密码)。 服务器采用这些参数,并根据需要对其进行处理。

这听起来似乎很明显,但是您说所有教程都使用php script ,所以您似乎很困惑:您在Android上遇到问题了吗? 还是您的服务器问题?

不论服务器技术(asp,cgi,jsp,php ...)和数据库(MySql,Oracle ...)如何,Android应用程序中所需的代码都是完全相同的,因为HTTP协议是标准的。

这是我从此处复制的一个示例,该示例使用两个POST参数发出了一个简单的HTTP请求。

public void postData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("id", "12345"));
        nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
} 

对于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(); }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM