简体   繁体   中英

Android Java connect to online database

Okay so i have a database hosted with 1and1.com from my website..

How can i from android connect the database and store info and also retrieve info from the app?

You can't access a remote database from your android directly. From what I understand, you need to do CRUD operations on the server DB ie you want the changes to be reflected on the server side. The best way to do this is to expose your CRUD operations over WebServices and consume them in your android app.

You need to write a service using either php or someother language which you are comfortable and return your sql reponse as either XML/JSON. From Android app you need to do HTTPURLConnection and using inbuit JSON/XML parsers you need to parse responses.

I assume that you know about web service. write a web service using .net and Then by using ksoap library you will get response. Then Format that response according to your requirement. code for reference:

private static final String NAMESPACE = "http://tempuri.org/"; 
private static final String URL ="http://localhost/Web_Service.asmx?";// you can use   IP address instead of localhost
private static final String METHOD_NAME = "Function_Name";
private static final String SOAP_ACTION = NAMESPACE+METHOD_NAME;

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
request.addProperty("parm_name",prm_value);// Parameter for Method
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);//call the eb service Method
} catch (Exception e) {
e.printStackTrace();}//Next task is to get Response and format that response
SoapObject obj,obj1,obj2,obj3;
obj= (SoapObject) envelope.getResponse();
obj1=(SoapObject) obj.getProperty("diffgram");
obj2=(SoapObject) obj1.getProperty("NewDataSet");
for(int i=0;i<obj2.getPropertyCount();i++)//the method getPropertyCount() return the number of rows
{
obj3=(SoapObject) obj2.getProperty(i);  
obj3.getProperty(0).toString();//value of column 1
obj3.getProperty(1).toString();//value of column 2
//like that you will get value from each column
}

Link for Ksoap library for Android

You can Download Library from above link. I hpoe this will solve your problem. Add comment if you have any problem regarding that.

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