Can an Android App connect directly to an online mysql database -


android 3.3 api 18 

hello,

i developing first app using android. app have connect online database store user data.

i looking cloud storage mysql database included. however, can app connect directly mysql database , push , pull data it. or there other things need do?

many suggestions,

yes can that.

materials need:

  1. webserver
  2. a database stored in webserver
  3. and little bit android knowledge :)
  4. webservices (json ,xml...etc) whatever comfortable

1. first set internet permissions in manifest file

 <uses-permission android:name="android.permission.internet" /> 

2. make class make httprequest server (i using json parisng values)

for eg:

    public class jsonfunctions {      public static jsonobject getjsonfromurl(string url) {         inputstream = null;         string result = "";         jsonobject jarray = null;          // download json data url         try {             httpclient httpclient = new defaulthttpclient();             httppost httppost = new httppost(url);             httpresponse response = httpclient.execute(httppost);             httpentity entity = response.getentity();             = entity.getcontent();          } catch (exception e) {             log.e("log_tag", "error in http connection " + e.tostring());         }          // convert response string         try {             bufferedreader reader = new bufferedreader(new inputstreamreader(                     is, "iso-8859-1"), 8);             stringbuilder sb = new stringbuilder();             string line = null;             while ((line = reader.readline()) != null) {                 sb.append(line + "\n");             }             is.close();             result = sb.tostring();         } catch (exception e) {             log.e("log_tag", "error converting result " + e.tostring());         }          try {              jarray = new jsonobject(result);         } catch (jsonexception e) {             log.e("log_tag", "error parsing data " + e.tostring());         }          return jarray;     } } 

3. in mainactivity make object of class jsonfunctions , pass url argument want data

eg:

jsonobject jsonobject; 

jsonobject = jsonfunctions.getjsonfromurl("http://your_database_url");

4. , read jsontags , store values in arraylist , later show in listview if want

and if have problem can follow blog gives excellent android tutorials androidhive

since above answer wrote long , httpclient, httppost,httpentity have been removed in api 23. can use below code in build.gradle(app-level) still continue using org.apache.httpin project.

android {     uselibrary 'org.apache.http.legacy'     signingconfigs {}     buildtypes {} } 

or can use httpurlconnection below response server

public string getjson(string url, int timeout) { httpurlconnection c = null; try {     url u = new url(url);     c = (httpurlconnection) u.openconnection();     c.setrequestmethod("get");     c.setrequestproperty("content-length", "0");     c.setusecaches(false);     c.setallowuserinteraction(false);     c.setconnecttimeout(timeout);     c.setreadtimeout(timeout);     c.connect();     int status = c.getresponsecode();      switch (status) {         case 200:         case 201:             bufferedreader br = new bufferedreader(new inputstreamreader(c.getinputstream()));             stringbuilder sb = new stringbuilder();             string line;             while ((line = br.readline()) != null) {                 sb.append(line+"\n");             }             br.close();             return sb.tostring();     }  } catch (malformedurlexception ex) {     logger.getlogger(getclass().getname()).log(level.severe, null, ex); } catch (ioexception ex) {     logger.getlogger(getclass().getname()).log(level.severe, null, ex); } {    if (c != null) {       try {           c.disconnect();       } catch (exception ex) {          logger.getlogger(getclass().getname()).log(level.severe, null, ex);       }    } } return null; 

}

or can use 3rd party library volley, retrofit call webservice api , response , later parse using fasterxml-jackson, google-gson.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -