2013-05-17 98 views
4

在使用中ksoap2庫的複雜響應所有我解析以下類型的響應與使用Ksoap2庫,但沒有獲得成功,得到結果我的要求是這樣的:如何分析與Android的

      <soapenv:Header/> 
          <soapenv:Body> 
           <tem:Register> 
           <tem:user>   
            <jir:Area>testArea</jir:Area> 
            <jir:AvailableBalance>0</jir:AvailableBalance> 
            <jir:CityId>1</jir:CityId> 
            <jir:Email>[email protected]</jir:Email> 
            <jir:FullName></jir:FullName> 
            <jir:Gender>M</jir:Gender> 
            <jir:Mobile>111111111</jir:Mobile> 
            <jir:Password>acxcsgsdvs</jir:Password> 
            <jir:Phone>111111111</jir:Phone> 
            <jir:SecurityAnswer>testQ</jir:SecurityAnswer> 
            <jir:SecurityQuestion>TestAb</jir:SecurityQuestion> 
            <jir:Username>sdf</jir:Username> 
           </tem:user> 
           </tem:Register> 
          </soapenv:Body> 

與像這樣利用ksoap2庫我已經成功創建的響應:

  Register{user{Area=test; AvailableBalance=150; CityId=1; [email protected]; FullName=Priyank; Gender=M; Mobile=9909957148; Password=testp; Phone=9909957148; SecurityAnswer=MyAns; SecurityQuestion=MyQues; Username=t; }} 

但我的問題是我的價值觀不是在用戶標籤獲取添加,所以我得到這樣的例外:

 Object reference not set to an instance of an object. 

請給我這個回覆如何解析這種類型的反應與使用kso​​ap2庫。

這是我的助手類裏面我是用解析反應:

public class KSOAPHelper { 

    private static final String TAG = "KSOAPHelper : KSOAP Helper"; 

    private static final String SOAP_ACTION = "http://tempuri.org/IUserService/"; 
    private static final String NAMESPACE = "http://tempuri.org/"; 
    private static final String URL = "my url"    

    // Method names 
    public static final String REGISTER = "Register"; 
    public static final String LOGIN = "Login"; 
    public static final String LOGOUT = "Logout"; 
    public static final String PROFILEDETAILS = "ProfileDetails"; 

    public static Object getResponce(LinkedHashMap<String, String> inputParams, String  methodName, final Context context) { 

     if (Utility.isConnectionAvailable) { 

      final String soap_action = SOAP_ACTION + methodName; 

      Log.d(TAG, soap_action); 

      SoapObject request = new SoapObject(NAMESPACE, methodName); 

      SoapObject user = new SoapObject(NAMESPACE, "user"); 

      for (String param : inputParams.keySet()) { 

       Log.d(TAG, param + " : " + inputParams.get(param)); 
       user.addProperty(param, inputParams.get(param)); 
      } 

      request.addSoapObject(user);    

      Log.d(TAG, "SOAP Request : " + request); 
      /* 
      * Set the web service envelope 
      */ 
      SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 

      soapEnvelope.dotNet = true; 
      // soapEnvelope.implicitTypes = true; 
      soapEnvelope.setOutputSoapObject(request); 
      HttpTransportSE httpTransportSE = new HttpTransportSE(URL); 

      try { 
       httpTransportSE.call(soap_action, soapEnvelope); 
       if (soapEnvelope.bodyIn instanceof SoapFault) { 
        String strFault = ((SoapFault) soapEnvelope.bodyIn).faultstring; 
        Log.v(TAG, "Fault string : " + strFault); 
       } else { 
        Object object = soapEnvelope.getResponse(); 

        return object; 
       } 

      } catch (Exception e) { 
       if (e instanceof SocketException || e instanceof IOException) { 
        if (context instanceof Activity) { 
         ((Activity) context).runOnUiThread(new Runnable() { 

          @Override 
          public void run() { 

          } 
         }); 
        } 
       } 
       e.printStackTrace(); 
      } 
     } else { 

      if (context instanceof Activity) { 
       ((Activity) context).runOnUiThread(new Runnable() { 

        @Override 
        public void run() { 

        } 
       }); 
      } 

      Log.d(TAG, "Internet Connection is not available"); 
     } 
     return null; 
    } 
+1

發生什麼異常?這個例外意味着一個對象還沒有被初始化。 –

+0

Tnx的答覆Janene我跟蹤這個概率,發現當我創建請求,並打電話我所有的數據都設置爲空,並獲得「用戶名不應該爲空」的響應,所以在我的請求中有些事情是錯誤的,即使我傳遞正確參數服務器獲取所有參數的空值。 –

回答

2

最後我得到了與使用KvmSerializable類的解決方案,現在我能夠嵌套對象上也與Ksoap2我在這裏工作分享我的代碼是工作的罰款

UserDetail Serializable類添加用戶標籤:

public class UserDetail implements KvmSerializable { 

     public String Area = null; 
     public Long AvailableBalance = null; 
     public Long CityId = null; 
     public String Email = null; 
     public String FullName = null; 
     public String Gender = null; 
     public String Mobile = null; 
     public String Password = null; 
     public String Phone = null; 
     public String SecurityAnswer = null; 
     public String SecurityQuestion = null; 
     public String Username; 
     private final String DATA_NAMESPACE = "http://schemas.datacontract.org/2004/07/Jiring.MTicketing.Models"; 

     public UserDetail() { 
     } 

     @Override 
     public Object getProperty(int arg0) { 

      switch (arg0) { 
      case 0: 
       return this.Area; 
      case 1: 
       return this.AvailableBalance; 
      case 2: 
       return this.CityId; 
      case 3: 
       return this.Email; 
      case 4: 
       return this.FullName; 
      case 5: 
       return this.Gender; 
      case 6: 
       return this.Mobile; 
      case 7: 
       return this.Password; 
      case 8: 
       return this.Phone; 
      case 9: 
       return this.SecurityAnswer; 
      case 10: 
       return this.SecurityQuestion; 
      case 11: 
       return this.Username; 
      } 

      return null; 
     } 

     @Override 
     public int getPropertyCount() { 
      return 12; 
     } 

     @SuppressWarnings("rawtypes") 
     @Override 
     public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) { 

      arg2.namespace = DATA_NAMESPACE; 

      switch (arg0) { 
      case 0: 
       arg2.type = PropertyInfo.STRING_CLASS; 
       arg2.name = "Area"; 
       break; 
      case 1: 
       arg2.type = PropertyInfo.LONG_CLASS; 
       arg2.name = "AvailableBalance"; 
       break; 
      case 2: 
       arg2.type = PropertyInfo.LONG_CLASS; 
       arg2.name = "CityId"; 
       break; 
      case 3: 
       arg2.type = PropertyInfo.STRING_CLASS; 
       arg2.name = "Email"; 
       break; 
      case 4: 
       arg2.type = PropertyInfo.STRING_CLASS; 
       arg2.name = "FullName"; 
       break; 
      case 5: 
       arg2.type = PropertyInfo.STRING_CLASS; 
       arg2.name = "Gender"; 
       break; 
      case 6: 
       arg2.type = PropertyInfo.STRING_CLASS; 
       arg2.name = "Mobile"; 
       break; 
      case 7: 
       arg2.type = PropertyInfo.STRING_CLASS; 
       arg2.name = "Password"; 
       break; 
      case 8: 
       arg2.type = PropertyInfo.STRING_CLASS; 
       arg2.name = "Phone"; 
       break; 
      case 9: 
       arg2.type = PropertyInfo.STRING_CLASS; 
       arg2.name = "SecurityAnswer"; 
       break; 
      case 10: 
       arg2.type = PropertyInfo.STRING_CLASS; 
       arg2.name = "SecurityQuestion"; 
       break; 
      case 11: 
       arg2.type = PropertyInfo.STRING_CLASS; 
       arg2.name = "Username"; 
       break; 
      default: 
       break; 
      } 

     } 

     @Override 
     public void setProperty(int arg0, Object arg1) { 

      switch (arg0) { 
      case 0: 
       this.Area = arg1.toString(); 
       break; 
      case 1: 
       this.AvailableBalance = Long.parseLong(arg1.toString()); 
       break; 
      case 2: 
       this.CityId = Long.parseLong(arg1.toString()); 
       break; 
      case 3: 
       this.Email = arg1.toString(); 
       break; 
      case 4: 
       this.FullName = arg1.toString(); 
       break; 
      case 5: 
       this.Gender = arg1.toString(); 
       break; 
      case 6: 
       this.Mobile = arg1.toString(); 
       break; 
      case 7: 
       this.Password = arg1.toString(); 
       break; 
      case 8: 
       this.Phone = arg1.toString(); 
       break; 
      case 9: 
       this.SecurityAnswer = arg1.toString(); 
       break; 
      case 10: 
       this.SecurityQuestion = arg1.toString(); 
       break; 
      case 11: 
       this.Username = arg1.toString(); 
       break; 
      default: 
       break; 
      } 

     } 

    } 

現在我Ksoap2輔助類設置HTTP調用:

public class KSOAPHelper { 

    private static final String TAG = "KSOAPHelper : KSOAP Helper"; 

    private static final String SOAP_ACTION = "http://tempuri.org/IUserService/"; 
     private static final String NAMESPACE = "http://tempuri.org/"; 
     private static final String URL = "my url"    


    /* 
    * This method is use to getRespnce from request which return objects. 
    */ 

    public static Object getResponce(SoapObject request, String METHOD, final Context context) { 

     if (Utility.isConnectionAvailable) { 

      final String soap_action = SOAP_ACTION + METHOD; 

      Log.d(TAG, "SOAP Request : " + request); 

      /* 
      * Set the web service envelope 
      */ 
      SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 

      soapEnvelope.dotNet = true; 
      soapEnvelope.setAddAdornments(false); 
      soapEnvelope.implicitTypes = true; 
      soapEnvelope.setOutputSoapObject(request); 

      /* 
      * Calling of the web service and retrieve result. 
      */ 

      HttpTransportSE httpTransportSE = new HttpTransportSE(URL); 

      try { 
       // Make the soap call. 
       httpTransportSE.call(soap_action, soapEnvelope); 
       // Get the SoapResult from the envelope body. 
       if (soapEnvelope.bodyIn instanceof SoapFault) { 
        String strFault = ((SoapFault) soapEnvelope.bodyIn).faultstring; 
        Log.d(TAG, "SOAP Request : " + httpTransportSE.requestDump); 
        Log.v(TAG, "Fault string : " + strFault); 
       } else { 
        Object object = soapEnvelope.getResponse(); 

        return object; 
       } 

      } catch (Exception e) { 
       Log.d(TAG, "SOAP Request : " + httpTransportSE.requestDump); 

       if (e instanceof SocketException || e instanceof IOException) { 
        if (context instanceof Activity) { 
         ((Activity) context).runOnUiThread(new Runnable() { 

          @Override 
          public void run() { 
          } 
         }); 
        } 
       } 
       e.printStackTrace(); 
      } 
     } else { 

      if (context instanceof Activity) { 
       ((Activity) context).runOnUiThread(new Runnable() { 

        @Override 
        public void run() { 
        } 
       }); 
      } 

      Log.d(TAG, "Internet Connection is not available"); 
     } 
     return null; 
    } 

這種方法可以添加嵌套的對象,因此我wanted.hope這將有幫助的一些一誰擁有像me.thanks同樣的問題,將創建請求。