0

Android應用程序將在數據庫服務器中顯示外部數據庫(Oracle)的數據。它只能與Web服務(RESTful)一起使用,因此我決定在Java/Java EE本身中開發Web服務。 我不想用PHP或其他一些technologies.Pls建議我繼續這一點。如何從Android應用程序訪問Oracle數據庫。

+0

你看看SOAP? – Kris

+0

我對它進行了搜索。我發現只有RESTful支持Android,但SOAP純粹是基於XML .Android無法獲得XML格式的響應。 – Mukthi

回答

0

CouchDB現在非常流行,它與Android很好。 Watch this video.

Apache CouchDB是一個面向文檔的數據庫,可以使用JavaScript以MapReduce方式進行查詢和索引。 CouchDB還提供雙向衝突檢測和解決的增量複製。

CouchDB提供了一個RESTful JSON API,它可以從任何允許HTTP請求的環境訪問。有無數的第三方客戶端庫可以使您的編程語言更加輕鬆。

0
public class RestClient { 

    private ArrayList <NameValuePair> params; 
    private ArrayList <NameValuePair> headers; 

    private String url; 

    private int responseCode; 
    private String message; 

    private String response; 

    public String getResponse() { 
     return response; 
    } 

    public String getErrorMessage() { 
     return message; 
    } 

    public int getResponseCode() { 
     return responseCode; 
    } 

    public RestClient(String url) 
    { 
     this.url = url; 
     params = new ArrayList<NameValuePair>(); 
     headers = new ArrayList<NameValuePair>(); 
    } 

    public void AddParam(String name, String value) 
    { 
     params.add(new BasicNameValuePair(name, value)); 
    } 

    public void AddHeader(String name, String value) 
    { 
     headers.add(new BasicNameValuePair(name, value)); 
    } 

    public void Execute(RequestMethod method) throws Exception 
    { 
     switch(method) { 
      case GET: 
      { 
       //add parameters 
       String combinedParams = ""; 
       if(!params.isEmpty()){ 
        combinedParams += "?"; 
        for(NameValuePair p : params) 
        { 
         String paramString = p.getName() + "=" + URLEncoder.encode(p.getValue(),」UTF-8″); 
         if(combinedParams.length() > 1) 
         { 
          combinedParams += "&" + paramString; 
         } 
         else 
         { 
          combinedParams += paramString; 
         } 
        } 
       } 

       HttpGet request = new HttpGet(url + combinedParams); 

       //add headers 
       for(NameValuePair h : headers) 
       { 
        request.addHeader(h.getName(), h.getValue()); 
       } 

       executeRequest(request, url); 
       break; 
      } 
      case POST: 
      { 
       HttpPost request = new HttpPost(url); 

       //add headers 
       for(NameValuePair h : headers) 
       { 
        request.addHeader(h.getName(), h.getValue()); 
       } 

       if(!params.isEmpty()){ 
        request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); 
       } 

       executeRequest(request, url); 
       break; 
      } 
     } 
    } 

    private void executeRequest(HttpUriRequest request, String url) 
    { 
     HttpClient client = new DefaultHttpClient(); 

     HttpResponse httpResponse; 

     try { 
      httpResponse = client.execute(request); 
      responseCode = httpResponse.getStatusLine().getStatusCode(); 
      message = httpResponse.getStatusLine().getReasonPhrase(); 

      HttpEntity entity = httpResponse.getEntity(); 

      if (entity != null) { 

       InputStream instream = entity.getContent(); 
       response = convertStreamToString(instream); 

       // Closing the input stream will trigger connection release 
       instream.close(); 
      } 

     } catch (ClientProtocolException e) { 
      client.getConnectionManager().shutdown(); 
      e.printStackTrace(); 
     } catch (IOException e) { 
      client.getConnectionManager().shutdown(); 
      e.printStackTrace(); 
     } 
    } 

    private static String convertStreamToString(InputStream is) { 

     BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
     StringBuilder sb = new StringBuilder(); 

     String line = null; 
     try { 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       is.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return sb.toString(); 
    } 
} 
1

依賴於Web服務。它只是RESTful XML嗎? JAX-WS? JSON?不管怎樣,它應該像使用HTTP套接字調出並解析響應一樣簡單。

創建Web服務按照下面的鏈接

RESTful Web Services

使用使用了HTTPClient

public static String hitService(String host, int port, String path, String postBody) throws IOException { 
     HttpHost target = new HttpHost(host, port); 
     HttpClient client = new DefaultHttpClient(); 
     HttpGet get = new HttpGet(path); 
     HttpEntity results = null; 
     try { 
      HttpResponse response=client.execute(target, get); 
      results = response.getEntity(); 
      return EntityUtils.toString(results); 
     } catch (Exception e) { 
      throw new RuntimeException("Web Service Failure"); 
     } finally { 
      if (results!=null) 
       try { 
        results.consumeContent(); 
       } catch (IOException e) { 
        // empty, Checked exception but don't care 
       } 
     } 
    } 

Android Cookbook example

+0

感謝您的回覆。實際上,我在JSP,Servlet和Struts中有知識。現在我想設計一個Web服務,以便從Android應用程序訪問數據庫服務器中的Oracle數據庫。我想知道如何開發Web服務。 – Mukthi

相關問題