簡體   English   中英

從Java(Android)外部應用程序調用Web服務的方法

[英]Invoke a method of a web service from a Java (Android) external app

抱歉,如果這個問題太簡單了,但我不知道答案。我要做的是使用Java應用程序調用Web服務的方法。 在這里,您可以找到一個Web服務: http ://muovi.roma.it/ws/xml/autenticazione/1並且我想調用稱為“ autenticazione.Accedi”的方法:

我有一個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進行相同的操作...有人可以幫助我解決這個問題嗎?

謝謝

我建議您將此項目用作庫。

https://github.com/matessoftwaresolutions/AndroidHttpRestService

它使您輕松處理api,控制網絡問題等。

您可以在此處找到使用示例。

您只需要:

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

希望對您有所幫助!!!

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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM