2012-07-12 41 views
1

我正在研究一個必須將數據存儲在數據庫中的Android應用程序。 此數據庫還必須通過iPhone應用程序和Web應用程序訪問。 我想用這個Mysql,因爲它是開源的。如何將android連接到遠程數據庫

我正在尋找這方面的一些信息,顯然我需要做一個連接到數據庫的web服務 。可能在php中,但我沒有任何使用PHP的經驗...

如何寫這個web服務,我在哪裏存儲它?我在哪裏製作和存儲數據庫? ...

任何人都可以幫助我解決這個問題嗎?

TNX

+0

這是一個很廣泛的問題的htdocs目錄。你想要做的是在這本書中解釋:http://www.sitepoint.com/books/phpmysql5/ – Caner 2012-07-12 10:42:36

回答

3

使用SQL Server來管理你的桌面上的數據,並在.NET中創建的Visual Studio中的Web服務。

然後連接到應用程序中的Web服務並設置/從數據庫中獲取數據。

鏈接就如何使.NET中的Web服務(不包括在Android上實現):如何爲你服務與Android連接http://srikanthtechnologies.com/blog/dotnet/wsdaljava.aspx

鏈接:http://seesharpgears.blogspot.in/2010/11/basic-ksoap-android-tutorial.html

http://www.codeproject.com/Articles/304302/Calling-Asp-Net-Webservice-ASMX-From-an-Android-Ap

http://adrianandroid.blogspot.in/2012/05/access-c-net-web-service-in.html

+0

請按照以下鏈接並開始編碼。如果遇到任何困難,請發佈您的問題。乾杯。 :) – Swayam 2012-07-12 10:44:19

+0

+1也爲webservice <->客戶端數據交換協議我會建議使用JSON – Cata 2012-07-12 10:45:42

+0

其他數據庫和技術可用... – DaveRlz 2012-07-12 10:46:41

1

我的自定義類將聯繫本地主機服務器(WAMP或XAMP服務器)

CustomHttpClient.java

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.URI; 
import java.util.ArrayList; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.conn.params.ConnManagerParams; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.params.HttpConnectionParams; 
import org.apache.http.params.HttpParams; 

public class CustomHttpClient { 
    /** The time it takes for our client to timeout */ 
    public static final int HTTP_TIMEOUT = 30 * 1000; // milliseconds 

    /** Single instance of our HttpClient */ 
    private static HttpClient mHttpClient; 

    /* 
    * Get our single instance of our HttpClient object. 
    * 
    * @return an HttpClient object with connection parameters set 
    */ 
    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; 
    } 

    /* 
    * Performs an HTTP Post request to the specified url with the specified 
    * parameters. 
    * 
    * @param url The web address to post the request to 
    * 
    * @param postParameters The parameters to send via the request 
    * 
    * @return The result of the request 
    * 
    * @throws Exception 
    */ 
    public static String executeHttpPost(String url, 
    ArrayList<NameValuePair> postParameters) throws Exception { 

     BufferedReader in = null; 
     try 
     { 
      HttpClient client = getHttpClient(); 
      HttpPost request = new HttpPost("http://192.168.1.38/" +url (pathoffilenameyouarecalling)); 
      UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters); 
      request.setEntity(formEntity); 
      HttpResponse response = client.execute(request); 
      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(); 

      String result = sb.toString(); 
      return result; 
     } finally { 
      if (in != null) { 
       try { 
        in.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 

    /** 
    * Performs an HTTP GET request to the specified url. 
    * 
    * @param url 
    *   The web address to post the request to 
    * @return The result of the request 
    * @throws Exception 
    */ 
    public static String executeHttpGet(String url) throws Exception { 
     BufferedReader in = null; 
     try { 
      HttpClient client = getHttpClient(); 
      HttpGet request = new HttpGet(); 
      request.setURI(new URI(url)); 
      HttpResponse response = client.execute(request); 
      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(); 

      String result = sb.toString(); 
      return result; 
     } finally { 
      if (in != null) { 
       try { 
        in.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 
} 

類調​​用此將實現這樣的方式。

ArrayList<NameValuePair> postParameter = new ArrayList<NameValuePair>(); 
postParameter.add(new BasicNameValuePair("parametertobesent",value)); 
respons = CustomHttpClient.executeHttpPost("urlofservice",postParameter); 

PHP文件應該是存在於XAMP服務器