简体   繁体   中英

Android connectivity with MySql 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 make registration form and login form of the Android application that interface with same MySQL database. But how?

I did find many tutorials but all are using PHP scripts. I'm using Eclipse for web development and Android Studio for android app.

This will involve at least 4 steps

  1. Create a POJO that will represent your data.

     public class LoginData implements Serializable{ public String loginName; public String loginPassword; //all other attributes that you need to send over HTTP } 
  2. Create s simple HTTP client (either using Java API, Apache HTTP client or some other library). My example is using Apache

    //you only need 1 client so make it static

     public static DefaultHttpClient getHttpClient(int timeout) { DefaultHttpClient client = null; SchemeRegistry Current_Scheme = new SchemeRegistry(); Current_Scheme.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); HttpParams Current_Params = new BasicHttpParams(); //set initial params HttpConnectionParams.setConnectionTimeout(Current_Params, timeout); HttpConnectionParams.setSoTimeout(Current_Params, timeout); ThreadSafeClientConnManager Current_Manager = new ThreadSafeClientConnManager(Current_Params, Current_Scheme); client = new DefaultHttpClient(Current_Manager, Current_Params); return client; } 
  3. In order to send your POJO, you should first serialize it (the first argument is Object so that it can accept any type, not just LoginData) //The data will be formatted as application/x-www-form in this way (BTW, web apps use this by default)

     HttpEntity serializePOJO(Object o,String encoding) List<NameValuePair> postParams = new ArrayList<NameValuePair>(); Map<String, Object> maps=objectMapper.convertValue(o, Map.class); Set<String> keys=maps.keySet(); Iterator<String> itr=keys.iterator(); while(itr.hasNext()){ String key=itr.next(); Object value=maps.get(key); if(value!=null){ postParams.add(new BasicNameValuePair(key, value.toString())); } } UrlEncodedFormEntity data=null; try { data = new UrlEncodedFormEntity(postParams,encoding);//egUTF-8 } catch (UnsupportedEncodingException e) { throw e; } return data; 

    }

  4. Issue a HTTP request

     public void checkLogin(LoginData data)throws Exception { String url = "localhost:8080/myApp/login"; HttpPost request=null; try{ request=new HttpPost(url); request.setEntity(serializePOJO(data)); HttpResponse response=getHttpClient(10000).execute(hg); if(response.getStatusLine().getStatusCode()!=200){ //handle exception } }finally{ if(request!=null){ request.releaseConnection(); } } } 

You can initialize objectMapper as

    objectMapper=new ObjectMapper();
    objectMapper.configure(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);

Your servlet should connect to the database and do all the processing , not the JSP page.

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