Android Java connect to online database

前端 未结 3 1236
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-11 13:53

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?

相关标签:
3条回答
  • 2020-12-11 14:07

    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.

    0 讨论(0)
  • 2020-12-11 14:10

    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.

    0 讨论(0)
  • 2020-12-11 14:19

    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 i.e. 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.

    0 讨论(0)
提交回复
热议问题