【问题标题】:Need working example of how to call/consume Java Web service from an Android Activity需要如何从 Android Activity 调用/使用 Java Web 服务的工作示例
【发布时间】:2012-05-24 03:35:08
【问题描述】:

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

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

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

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

提前感谢您的帮助。

【问题讨论】:

    标签: android service android-activity web wsdl


    【解决方案1】:

    这是一个“如何从 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(法国网站)

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

      您可以使用以下所有方法在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("your web URL","your data any you want to save in database"); 调用这个方法;
      • 您的网络服务中有方法可以与服务器交互,您可以使用此方法创建 url 并将其作为参数传递给我上面提到的方法
      猜你喜欢
      • 2014-12-14
      • 1970-01-01
      • 1970-01-01
      • 2016-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-28
      • 2011-08-28
      相关资源
      最近更新 更多