简体   繁体   中英

Android application database connectivity with web app using Jsp

I am developing a web app in JSP. For same project I'm developing an Android app. The web app uses Apache Tomcat and MySQL. Now I want to log in from the Android application by retrieving data from MySQL database. But how?

I did find many tutorials but all are using PHP scripts. I'm using Eclipse for both apps.

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.

Usually a client (either an app or a web browser) makes an HTTP request sending parameters (eg login, password) with POST or GET methods. 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? 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.

Here is an example I copied from here to make a simple HTTP request with two POST parameters.

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.

           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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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