2

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

这是我到目前为止所做的。

创建了一个 Android 活动。在 Eclipse 中创建的类计算给定半径的区域并返回值。为上述类创建了一个 Web 服务,并使用 WebService Explorer 进行了测试,它可以工作。Web 服务将端口 11144 用于请求,将 8080 用于响应。

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

在此先感谢您的帮助。

4

2 回答 2

0

这是“如何从 Android Activity 调用/使用 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();
}

我使用这个库来联系服务: http ://code.google.com/p/ksoap2-android/

来源:http ://www.thinkdroid.eu/?p=192 (法文网站)

我希望我对你有帮助。

于 2012-05-16T14:55:51.657 回答
0

您可以使用以下所有方法在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();
        }
于 2012-05-16T15:35:29.390 回答