2014-10-11 84 views
0

如果問題太簡單了,但我不知道答案。 我所要做的就是調用Web服務的方法使用一個Java應用程序。 在這裏你可以找到一個Web服務: http://muovi.roma.it/ws/xml/autenticazione/1 我想調用名爲「autenticazione.Accedi:」方法從Java(Android)外部應用調用Web服務的方法

我有一個這樣做的一個pyhton例如:

from xmlrpclib import Server 
from pprint import pprint 

DEV_KEY = 'Inserisci qui la tua chiave' 

s1 = Server('http://muovi.roma.it/ws/xml/autenticazione/1') 
s2 = Server('http://muovi.roma.it/ws/xml/paline/7') 

token = s1.autenticazione.Accedi(DEV_KEY, '') 

res = s2.paline.Previsioni(token, '70101', 'it') 
pprint(res) 

但我需要的Java中的相同操作...任何人都可以幫助我解決這個問題嗎?

謝謝

+0

它是什麼樣的方法(GET,POST ..)? – 2014-10-11 23:46:22

+0

居然,我不知道 – fancoolo 2014-10-11 23:49:36

回答

0

我建議你使用這個項目作爲一個庫。

https://github.com/matessoftwaresolutions/AndroidHttpRestService

它讓您能夠輕鬆處理的API,控制網絡的問題等

你可以找到使用的樣本存在。

你只需要:

  • 構建您的網址
  • 告訴在POST執行組件/ GET等模式
  • 構建您的JSON

我希望它能幫助! !

+0

我會檢查...謝謝分享 – fancoolo 2014-10-11 23:45:03

+1

我已經開發了這個組件。每個使用它的人都會發現它很容易,因爲解釋(README)是一個示例。如果您需要更多幫助,我可以儘快回覆您。問我任何你需要的。 – 2014-10-11 23:47:23

+0

我還沒有嘗試過,但有了這個庫可以調用Web服務的方法嗎? – fancoolo 2014-10-11 23:52:13

-1
package com.example.jojo.gridview; 
import android.util.Log; 
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.List; 
/** 
* Created by jojo on 12/10/15. 
*/ 
public class WebService { 
    String url="http://192.168.1.15/Travel_Dairy/"; 


    String invokeGetWebservice(String webUrl) 
    { 
     String result = ""; 
     webUrl=webUrl.replace(" ","%20"); 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpGet httpget = new HttpGet(webUrl); 
     HttpResponse response; 
     try { 
      response = httpclient.execute(httpget); 
      HttpEntity entity = response.getEntity(); 
      if (entity != null) { 
       InputStream inputstream= entity.getContent(); 
       BufferedReader bufferedreader = new BufferedReader(
         new InputStreamReader(inputstream), 2 * 1024); 
       StringBuilder stringbuilder = new StringBuilder(); 
       String currentline = null; 
       try { 
        while ((currentline = bufferedreader.readLine()) != null) { 
         stringbuilder.append(currentline + "\n"); 
        } 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       result = stringbuilder.toString(); 
       Log.e("Result", result); 
       inputstream.close(); 
       return result; 
      } 
     } catch (ClientProtocolException e1) { 

      Log.e("ClientProtocolException", e1.toString()); 
      return result; 

     } catch (IOException e1) { 

      Log.e("IOException", e1.toString()); 
      return result; 

     } 
     return result; 
    } 
    public List<DataModel> getTrips() { 
     String getname="view_details.php?"; 
     String completeurlforget=url+getname; 
     //String seturl= "ur_id="+userid; 
     //String finalurl=completeurlforget+seturl; 
     String result=invokeGetWebservice(completeurlforget); 
     try { 
      JSONArray jsonarry=new JSONArray(result); 
      List<DataModel> ar=new ArrayList(); 
      for(int i=0;i<jsonarry.length();i++) 
      { 
       JSONObject jsonobj=jsonarry.getJSONObject(i); 
       DataModel user=new DataModel(); 
       user.setName(jsonobj.getString("name")); 
       user.setImage(jsonobj.getString("image")); 
       ar.add(user); 
      } 
      return ar; 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 
} 
+0

你已經提供了一個代碼塊,但沒有給出任何其他反饋。考慮在代碼中添加一些解釋以使其成爲更好的答案。 – chembrad 2015-10-14 01:58:10