2015-07-10 44 views
3

得到這個數組在Java中的每一個元素,我得到陣列的串上responseJSON這樣如何使用Android的stuio

Result: responseJSON = ["Product1","Product2","Product1","Product2"] 

try { 
     // Invole web service 
     androidHttpTransport.call(SOAP_ACTION+methName, envelope); 
     // Get the response 
     SoapPrimitive response = (SoapPrimitive) envelope.getResponse(); 
     // Assign it to static variable 
     responseJSON = response.toString(); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

怎麼能獲得陣列中的每個元素,這樣我可以用它在我的ListView上顯示?

由於

public void invokeJSONWS(String country, String methName) { 
    // Create request 
    SoapObject request = new SoapObject(NAMESPACE, methName); 
    /* 
    // Property which holds input parameters 
    PropertyInfo paramPI = new PropertyInfo(); 
    // Set Name 
    paramPI.setName("country"); 
    // Set Value 
    paramPI.setValue(country); 
    // Set dataType 
    paramPI.setType(String.class); 
    // Add the property to request object 
    request.addProperty(paramPI); 
    */ 
    // Create envelope 
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
      SoapEnvelope.VER11); 
    envelope.dotNet = true; 
    // Set output SOAP object 
    envelope.setOutputSoapObject(request); 
    // Create HTTP call object 
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 

    try { 
     // Invole web service 
     androidHttpTransport.call(SOAP_ACTION+methName, envelope); 
     // Get the response 
     SoapPrimitive response = (SoapPrimitive) envelope.getResponse(); 
     // Assign it to static variable 
     responseJSON = response.toString(); 

     JSONArray responseArr = new JSONArray(responseJSON); 
     for(int i=0;i < responseArr.length();i++) 
     { 
      String temp=responseArr.getString(i); 
      myProduct.add(new Product(responseJSON,2,R.drawable.user_awake,responseJSON)); 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

這是我用它是否正確,我還包括在列表視圖中添加項目的方法?

請幫助

感謝

+1

的可能重複[如何解析Android中的JSON(http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android) –

+0

問題在於你在你的對象中添加responseJSON而不是你已經解析過的temp變量。 –

回答

2

通過這個字符串JSONAarry構造這樣

ArrayList<String> mParsedList = new ArrayList<String>(); 
    JSONAarry responseArr=new JSONArray(responseJSON); 
    for(int i=0;i<responseArr.length;i++) 
    { 
     String temp=responseArr.getString(i);// get one by one element 
     myProduct.add(new Product(temp,2,R.drawable.user_awake,responseJSON)); 
    } 
+0

我在String temp = responseArr [i]; –

+1

什麼類型的錯誤,後期 –

+0

預期的數組類型;發現:'org.json.JSONArray' –