2014-10-11 85 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