简体   繁体   English

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

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

I am developing a web app in JSP. 我正在用JSP开发一个Web应用程序。 For same project I'm developing an Android app. 对于同一项目,我正在开发一个Android应用程序。 The web app uses Apache Tomcat and MySQL. 该Web应用程序使用Apache Tomcat和MySQL。 Now I want to log in from the Android application by retrieving data from MySQL database. 现在,我想通过从MySQL数据库检索数据来从Android应用程序登录。 But how? 但是如何?

I did find many tutorials but all are using PHP scripts. 我确实找到了很多教程,但是都使用PHP脚本。 I'm using Eclipse for both apps. 我将Eclipse用于这两个应用程序。

What happens between the client (your Android app) and the server is loosely coupled, meaning that they are not related whatsoever except for the protocol with which they communicate, which for a web service is HTTP. 客户端 (您的Android应用)和服务器之间发生的事情是松散耦合的,这意味着它们之间没有任何关系,除了它们与之通信的协议(对于Web服务而言,协议是HTTP)。

Usually a client (either an app or a web browser) makes an HTTP request sending parameters (eg login, password) with POST or GET methods. 通常,客户端(应用程序或Web浏览器)会发出HTTP请求,以POST或GET方法发送参数(例如登录名,密码)。 The server takes these parameters and processes them according to its needs. 服务器采用这些参数,并根据需要对其进行处理。

This may sound obvious, but you say that all the tutorials are using php script , so you seem confused: your problem on Android? 这听起来似乎很明显,但是您说所有教程都使用php script ,所以您似乎很困惑:您在Android上遇到问题了吗? or is your problem in the server? 还是您的服务器问题?

The code you need in your Android app is EXACTLY THE SAME regardless of the server technology (asp, cgi, jsp, php...) and database (MySql, Oracle...), because the HTTP protocol is standard. 不论服务器技术(asp,cgi,jsp,php ...)和数据库(MySql,Oracle ...)如何,Android应用程序中所需的代码都是完全相同的,因为HTTP协议是标准的。

Here is an example I copied from here to make a simple HTTP request with two POST parameters. 这是我从此处复制的一个示例,该示例使用两个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
    }
} 

For android Try this. 对于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;     
    }   

And then 接着

                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;    
    }

Where 哪里

      public static String universal_URL_MENU = "http://192.***.1.@:9999/my_Project/ReqFromTabFor.do";

Now for Jsp or Servlet 现在适用于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