2016-05-11 6 views
-1

私はアンドロイドapp.the webserviceメソッドからsoap webserviceを呼び出す必要があります4つの値を持つオブジェクトがあります。私はksoapのlib.Butについて読んだ私はどのように混乱しているオブジェクトは、アンドロイドからパラメータとして送信されます。私はこれを解決するのを助けてください。soap webserviceメソッドは、アンドロイドから呼び出されるオブジェクトで

public SoapObject soap(String METHOD_NAME, String SOAP_ACTION, String NAMESPACE, String URL,String IP,String SERVICEPATH) throws IOException, XmlPullParserException 
    { 

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); //set up request 

    PropertyInfo pi = new PropertyInfo(); 

    pi.setName("Parameter1"); 
    pi.setValue(Value1); 

    request.addProperty(pi); 
    pi.setName("Parameter2"); 
    pi.setValue(Value2); 
    request.addProperty(pi); 



SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); // put all required data into a soap 
envelope.dotNet = true; 
envelope.setOutputSoapObject(request); // prepare request 
envelope.bodyOut = request; 
    Log.d("ENVELOPE",""+"Coming3"); 
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
//androidHttpTransport. 
androidHttpTransport.call(SOAP_ACTION, envelope); 

Log.d("ENVELOPE",""+envelope.bodyIn); 
SoapObject result = (SoapObject) envelope.bodyIn; // get response 
Log.d("ENVELOPE",""+envelope.bodyIn); 
SoapObject responseBodyRaw,responseBody,tableRow; 
return result; 
} 

これはどのように行う必要がありますか? 私はksoapとsoap webservicesで作業していません。助けてください。

+0

あなたのコードはどこですか?郵便番号少なくとも –

+0

はいKsoapのlibはgood.Howeverどのようなエラーを取得している/あなたはこれを見たことがありますか?http://programmerguru.com/android-tutorial/how-to-call-java-web-service-in -android/ –

+0

リクエストを送信する必要がある形式(構造)とは何ですか? –

答えて

0

カテゴリオブジェクトを返すようなwebmethodが定義されているとします。

[WebMethod] 
public Category GetCategoryById(Category C); 

セイカテゴリクラスは、KvmSerializableを実装する必要がKSoapでそれを使用するように区分、名称およびDescription.Soのような3つのプロパティを持っています。

package CommonObjects; 

import java.util.Hashtable; 
import org.ksoap2.serialization.KvmSerializable; 
import org.ksoap2.serialization.PropertyInfo; 

public class Category implements KvmSerializable 
{ 
public int CategoryId; 
public String Name; 
public String Description; 

public Category(){} 
public Category(int categoryId, String name, String description) { 

    CategoryId = categoryId; 
    Name = name; 
    Description = description; 
} 
public Object getProperty(int arg0) { 

    switch(arg0) 
    { 
    case 0: 
     return CategoryId; 
    case 1: 
     return Name; 
    case 2: 
     return Description; 
    } 

    return null; 
} 

public int getPropertyCount() { 
    return 3; 
} 
public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) { 
    switch(index) 
    { 
    case 0: 
     info.type = PropertyInfo.INTEGER_CLASS; 
     info.name = "CategoryId"; 
     break; 
    case 1: 
     info.type = PropertyInfo.STRING_CLASS; 
     info.name = "Name"; 
     break; 
    case 2: 
     info.type = PropertyInfo.STRING_CLASS; 
     info.name = "Description"; 
     break; 
    default:break; 
    } 
} 

public void setProperty(int index, Object value) { 
    switch(index) 
    { 
    case 0: 
     CategoryId = Integer.parseInt(value.toString()); 
     break; 
    case 1: 
     Name = value.toString(); 
     break; 
    case 2: 
     Description = value.toString(); 
     break; 
    default: 
     break; 
    } 
} 
} 

ここで、プロパティをマップして読み、書き込む必要があります。

ように、Webサービスの呼び出しは次のとおりです。

public void WebServiceCallExample() 
    { 
     String NAMESPACE = "http://vladozver.org/"; 
     String METHOD_NAME = "GetCategoryById"; 
     String SOAP_ACTION = "http://vladozver.org/GetCategoryById"; 
     String URL = "http://192.168.1.3/VipEvents/Services/CategoryServices.asmx"; 

     SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME); 

     /* 
     * Create Category with Id to be passed as an argument 
     * 
     * */ 
     Category C = new Category(); 
     C.CategoryId = 1; 

     /* 
     * Set the category to be the argument of the web service method 
     * 
     * */ 
     PropertyInfo pi = new PropertyInfo(); 
     pi.setName("C"); 
     pi.setValue(C); 
     pi.setType(C.getClass()); 
     Request.addProperty(pi); 

     /* 
     * Set the web service envelope 
     * 
     * */ 
     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
     envelope.dotNet = true; 
     envelope.setOutputSoapObject(Request); 

     envelope.addMapping(NAMESPACE, "Category",new Category().getClass()); 
     AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL); 
     /* 
     * Call the web service and retrieve result ... how luvly <3 
     * 
     * */ 
     try 
     { 
      androidHttpTransport.call(SOAP_ACTION, envelope); 
      SoapObject response = (SoapObject)envelope.getResponse(); 
      C.CategoryId = Integer.parseInt(response.getProperty(0).toString()); 
      C.Name = response.getProperty(1).toString(); 
      C.Description = (String) response.getProperty(2).toString(); 
      TextView tv = (TextView)findViewById(R.id.TextView01); 
      tv.setText("CategoryId: " +C.CategoryId + " Name: " + C.Name + " Description " + C.Description); 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 

このコードを使用するためには、あなたがあなた自身に、この方法では最初の三つの変数を置き換えることを確認してください。注:SOAP_ACTIONは実際にNAMESPACE + METHOD_NAMEとして計算することができます

プロパティ情報に関する行では、実際にカテゴリクラスの複雑なオブジェクトをKSOAPに渡す方法を示します。

PropertyInfo pi = new PropertyInfo(); 
pi.setName("Category"); 
pi.setValue(C); 
pi.setType(C.getClass()); 
Request.addProperty(pi); 

戻り値の型についてはウェブメソッドは、(例えば我々のような)複雑なオブジェクトを返す場合、あなたは応答を処理する方法をKSOAPを伝える必要があります。それは、次のコードで行われ:このblogpostから

androidHttpTransport.call(SOAP_ACTION, envelope); 
SoapObject response = (SoapObject)envelope.getResponse(); 
C.CategoryId = Integer.parseInt(response.getProperty(0).toString()); 
C.Name = response.getProperty(1).toString(); 
C.Description = (String) response.getProperty(2).toString(); 

リファレンス:

envelope.addMapping(NAMESPACE, "Category",new Category().getClass()); 

コードのこの部分は、応答の実際の検索です。

+0

こんにちは...ありがとうコードです。私は1つの疑問を持っています。私は、バイト[]とカテゴリのクラスの日付を持っているので...どのようなこれらの値の型にする必要があります – angh

+0

私は7値を追加する必要があります私はすべての値を設定する必要があります。 – angh

+0

こんにちは...私はxmlPullParseExceptionを取得しています:予期しないトークン@ 3:15 ...私を助けてください...それを解決してください – angh

関連する問題