2015-07-10 53 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' –