1

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.

allcaps
  • 10,945
  • 1
  • 33
  • 54
jaymin581
  • 165
  • 3
  • 15
  • Are you using JDBC? I think most of the DB related projects use them in Java. That has a tutorial: http://docs.oracle.com/javase/tutorial/jdbc/ Do you want to use the DB on Android, or in jsp? – Gábor Bakos Mar 09 '14 at 17:14
  • yes, I am using JDBC and I had created whole DB on jsp with mySql and i want ot only use that DB in android Application – jaymin581 Mar 09 '14 at 17:28
  • I do not understand what is your problem. For me it seems you have 2 options: connect to the mysql database through JDBC (http://stackoverflow.com/questions/15732853/how-to-connect-android-app-to-mysql-database) or create interface in JSP and call that interface from android. Are you trying something else? – Gábor Bakos Mar 09 '14 at 17:32

2 Answers2

0

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 (e.g. 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
    }
} 
Merlevede
  • 8,140
  • 1
  • 24
  • 39
0

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(); }
DJhon
  • 1,548
  • 3
  • 22
  • 39