需要如何从Android活动调用/使用Java Web服务的工作示例吗

需要如何从Android活动调用/使用Java Web服务的工作示例吗,android,service,android-activity,web,wsdl,Android,Service,Android Activity,Web,Wsdl,我是android开发新手,需要连接到服务器并获取android活动的信息或值。请提供如何从android活动调用/使用java Web服务的工作示例 以下是我迄今为止所做的工作 创建了一个Android活动。 在eclipse中创建的类,用于计算给定半径的面积并返回值。 为上面的类创建了一个web服务,并使用WebService Explorer进行了测试,结果正常。Web服务使用端口11144进行请求,使用端口8080进行响应 我的问题是: 我是使用wsdl还是使用Java客户端作为存根?

我是android开发新手,需要连接到服务器并获取android活动的信息或值。请提供如何从android活动调用/使用java Web服务的工作示例

以下是我迄今为止所做的工作

创建了一个Android活动。 在eclipse中创建的类,用于计算给定半径的面积并返回值。 为上面的类创建了一个web服务,并使用WebService Explorer进行了测试,结果正常。Web服务使用端口11144进行请求,使用端口8080进行响应

我的问题是: 我是使用wsdl还是使用Java客户端作为存根? 如果我需要使用存根,它是否像其他java类一样只是另一个impo


提前感谢您的帮助。

这是一个“如何从Android活动调用/使用web服务”的工作示例

这是联系服务的班级:

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.util.Log;

public class WebService extends SoapObject {

    // a remplacer par les bonnes informations
    private static final String SOAP_ACTION = "http://tempuri.org/GetChampionRotationWeek";

    // a remplacer par les bonnes informations
    private static final String METHOD_NAME = "GetChampionRotationWeek";

    // a remplacer par les bonnes informations
    private static final String NAMESPACE = "http://tempuri.org/";

    // a remplacer par les bonnes informations
    private static final String URL = "http://www.thinkdroid.eu/WebService1.asmx";

    public WebServiceLogin(String namespace, String name) {
        super(namespace, name);
    }

    public String getResult(){
        String resultat = "";
        try {
            SoapObject requete = new SoapObject(NAMESPACE, METHOD_NAME);
            //Soap version 1.1
            SoapSerializationEnvelope enveloppe = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            // Codé en dotNet ou non ?
            enveloppe.dotNet = true;
            enveloppe.setOutputSoapObject(requete);
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            androidHttpTransport.call(SOAP_ACTION, enveloppe);
            resultat = enveloppe.getResponse().toString();
        } catch (Exception e) {
            Log.e("Exception", "" + e);
            e.printStackTrace();
        }
        return resultat;
    }
}
这就是实例化和使用这个类的方法:

try {
    WebService serviceConnexion = new WebService("http://tempuri.org/", "WebService1Soap");
    result = serviceConnexion.getResult();
} catch (Exception e) {
    e.printStackTrace();
}
我使用此库联系服务:

资料来源:(法国网站)


希望我能为您提供帮助。

这是一个“如何从Android活动调用/使用web服务”的工作示例

这是联系服务的班级:

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.util.Log;

public class WebService extends SoapObject {

    // a remplacer par les bonnes informations
    private static final String SOAP_ACTION = "http://tempuri.org/GetChampionRotationWeek";

    // a remplacer par les bonnes informations
    private static final String METHOD_NAME = "GetChampionRotationWeek";

    // a remplacer par les bonnes informations
    private static final String NAMESPACE = "http://tempuri.org/";

    // a remplacer par les bonnes informations
    private static final String URL = "http://www.thinkdroid.eu/WebService1.asmx";

    public WebServiceLogin(String namespace, String name) {
        super(namespace, name);
    }

    public String getResult(){
        String resultat = "";
        try {
            SoapObject requete = new SoapObject(NAMESPACE, METHOD_NAME);
            //Soap version 1.1
            SoapSerializationEnvelope enveloppe = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            // Codé en dotNet ou non ?
            enveloppe.dotNet = true;
            enveloppe.setOutputSoapObject(requete);
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            androidHttpTransport.call(SOAP_ACTION, enveloppe);
            resultat = enveloppe.getResponse().toString();
        } catch (Exception e) {
            Log.e("Exception", "" + e);
            e.printStackTrace();
        }
        return resultat;
    }
}
这就是实例化和使用这个类的方法:

try {
    WebService serviceConnexion = new WebService("http://tempuri.org/", "WebService1Soap");
    result = serviceConnexion.getResult();
} catch (Exception e) {
    e.printStackTrace();
}
我使用此库联系服务:

资料来源:(法国网站)


希望我能为您提供帮助。

您可以使用以下所有方法在android中调用web服务

public String postData(String result, JSONObject obj) {
            // Create a new HttpClient and Post Header
            String InsertTransactionResult = null;
            HttpClient httpclient = new DefaultHttpClient();
            HttpParams myParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(myParams, 1000);
            HttpConnectionParams.setSoTimeout(myParams, 1000);

            try {

                HttpPost httppost = new HttpPost(result.toString());
                httppost.setHeader("Content-type", "application/json");
                StringEntity se = new StringEntity(obj.toString());
                se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
                        "application/json"));
                httppost.setEntity(se);

                HttpResponse response = httpclient.execute(httppost);
                InsertTransactionResult = EntityUtils
                        .toString(response.getEntity());

            } catch (ClientProtocolException e) {

            } catch (IOException e) {
            }
            return InsertTransactionResult;
        }

        public String putData(String result, JSONObject obj) {

            // Create a new HttpClient and Put Header

            String UpdateTransactionResult = null;
            HttpClient httpclient = new DefaultHttpClient();
            HttpParams myParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(myParams, 10000);
            HttpConnectionParams.setSoTimeout(myParams, 10000);

            try {

                HttpPut httpPut = new HttpPut(result.toString());
                httpPut.setHeader("Content-type", "application/json");
                StringEntity se = new StringEntity(obj.toString());
                se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
                        "application/json"));
                httpPut.setEntity(se);

                HttpResponse response = httpclient.execute(httpPut);
                UpdateTransactionResult = EntityUtils
                        .toString(response.getEntity());

            } catch (ClientProtocolException e) {

            } catch (IOException e) {
            }
            return UpdateTransactionResult;

        }


        public String deleteRecord(String result) {

            // Create a new HttpClient and Get Header

            StringBuilder builder = new StringBuilder();
            HttpClient client = new DefaultHttpClient();
            HttpDelete httpDelete = new HttpDelete(result.toString());

            try {
                HttpParams myParams = new BasicHttpParams();
                HttpConnectionParams.setConnectionTimeout(myParams, 10000);
                HttpConnectionParams.setSoTimeout(myParams, 0);
                HttpResponse response = client.execute(httpDelete);
                StatusLine statusLine = response.getStatusLine();
                int statusCode = statusLine.getStatusCode();

                System.out.println(response.toString());
                if (statusCode == 200) {
                    HttpEntity entity = response.getEntity();
                    InputStream content = entity.getContent();
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(content));
                    String line;
                    while ((line = reader.readLine()) != null) {
                        builder.append(line);
                    }
                } else {
                    Log.e(LoginActivity.class.toString(), "Failed to Authenticate");
                }

            } catch (ClientProtocolException e) {

            } catch (IOException e) {
                System.out.println(e);
            }
            return builder.toString();
        }



    public String getMethod(String result) {

            // Create a new HttpClient and Get Header

            StringBuilder builder = new StringBuilder();
            HttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(result.toString());

            try {
                HttpParams myParams = new BasicHttpParams();
                HttpConnectionParams.setConnectionTimeout(myParams, 0);
                HttpConnectionParams.setSoTimeout(myParams, 0);
                HttpResponse response = client.execute(httpGet);
                StatusLine statusLine = response.getStatusLine();
                int statusCode = statusLine.getStatusCode();

                System.out.println(response.toString());
                if (statusCode == 200) {
                    HttpEntity entity = response.getEntity();
                    InputStream content = entity.getContent();
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(content));
                    String line;
                    while ((line = reader.readLine()) != null) {
                        builder.append(line);
                    }
                } else {
                    Log.e(LoginActivity.class.toString(), "Failed to Authenticate");
                }

            } catch (ClientProtocolException e) {
            } catch (IOException e) {
            }
            return builder.toString();
        }

您可以使用以下所有方法在android中调用web服务

public String postData(String result, JSONObject obj) {
            // Create a new HttpClient and Post Header
            String InsertTransactionResult = null;
            HttpClient httpclient = new DefaultHttpClient();
            HttpParams myParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(myParams, 1000);
            HttpConnectionParams.setSoTimeout(myParams, 1000);

            try {

                HttpPost httppost = new HttpPost(result.toString());
                httppost.setHeader("Content-type", "application/json");
                StringEntity se = new StringEntity(obj.toString());
                se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
                        "application/json"));
                httppost.setEntity(se);

                HttpResponse response = httpclient.execute(httppost);
                InsertTransactionResult = EntityUtils
                        .toString(response.getEntity());

            } catch (ClientProtocolException e) {

            } catch (IOException e) {
            }
            return InsertTransactionResult;
        }

        public String putData(String result, JSONObject obj) {

            // Create a new HttpClient and Put Header

            String UpdateTransactionResult = null;
            HttpClient httpclient = new DefaultHttpClient();
            HttpParams myParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(myParams, 10000);
            HttpConnectionParams.setSoTimeout(myParams, 10000);

            try {

                HttpPut httpPut = new HttpPut(result.toString());
                httpPut.setHeader("Content-type", "application/json");
                StringEntity se = new StringEntity(obj.toString());
                se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
                        "application/json"));
                httpPut.setEntity(se);

                HttpResponse response = httpclient.execute(httpPut);
                UpdateTransactionResult = EntityUtils
                        .toString(response.getEntity());

            } catch (ClientProtocolException e) {

            } catch (IOException e) {
            }
            return UpdateTransactionResult;

        }


        public String deleteRecord(String result) {

            // Create a new HttpClient and Get Header

            StringBuilder builder = new StringBuilder();
            HttpClient client = new DefaultHttpClient();
            HttpDelete httpDelete = new HttpDelete(result.toString());

            try {
                HttpParams myParams = new BasicHttpParams();
                HttpConnectionParams.setConnectionTimeout(myParams, 10000);
                HttpConnectionParams.setSoTimeout(myParams, 0);
                HttpResponse response = client.execute(httpDelete);
                StatusLine statusLine = response.getStatusLine();
                int statusCode = statusLine.getStatusCode();

                System.out.println(response.toString());
                if (statusCode == 200) {
                    HttpEntity entity = response.getEntity();
                    InputStream content = entity.getContent();
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(content));
                    String line;
                    while ((line = reader.readLine()) != null) {
                        builder.append(line);
                    }
                } else {
                    Log.e(LoginActivity.class.toString(), "Failed to Authenticate");
                }

            } catch (ClientProtocolException e) {

            } catch (IOException e) {
                System.out.println(e);
            }
            return builder.toString();
        }



    public String getMethod(String result) {

            // Create a new HttpClient and Get Header

            StringBuilder builder = new StringBuilder();
            HttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(result.toString());

            try {
                HttpParams myParams = new BasicHttpParams();
                HttpConnectionParams.setConnectionTimeout(myParams, 0);
                HttpConnectionParams.setSoTimeout(myParams, 0);
                HttpResponse response = client.execute(httpGet);
                StatusLine statusLine = response.getStatusLine();
                int statusCode = statusLine.getStatusCode();

                System.out.println(response.toString());
                if (statusCode == 200) {
                    HttpEntity entity = response.getEntity();
                    InputStream content = entity.getContent();
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(content));
                    String line;
                    while ((line = reader.readLine()) != null) {
                        builder.append(line);
                    }
                } else {
                    Log.e(LoginActivity.class.toString(), "Failed to Authenticate");
                }

            } catch (ClientProtocolException e) {
            } catch (IOException e) {
            }
            return builder.toString();
        }

谢谢你的快速回复。请告诉我如何将参数传递给Java方法并返回结果。只需像postData一样调用此方法(“您的web URL”,“您想要保存在数据库中的任何数据”);您的Web服务中有一些方法可以与服务器连接,您可以使用这些方法创建url,并将其作为参数传递给我上面提到的方法,谢谢您的快速回复。请告诉我如何将参数传递给Java方法并返回结果。只需像postData一样调用此方法(“您的web URL”,“您想要保存在数据库中的任何数据”);您的Web服务中有一些方法可以与服务器连接,您可以使用这些方法创建url,并将其作为参数传递给我上面提到的方法