【问题标题】:how to get each element of this array in java using android stuio如何使用android studio在java中获取这个数组的每个元素
【发布时间】:2015-07-10 09:26:45
【问题描述】:

我在 responseJSON 上得到这样的数组字符串

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();
    }
}

这是我使用它的正确方法,我还包括在列表视图中添加项目??

请帮忙

谢谢

【问题讨论】:

标签: java android arrays json ksoap2


【解决方案1】:

像这样把这个字符串传递给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));
  }

【讨论】:

  • 预期数组类型;找到:'org.json.JSONArray'
  • 那是错字。我已经更新了我的答案,请检查一下。
  • 你能把你的解析方法贴在这里吗?
  • temp 添加到而不是responseJSON 立即查看
  • 您在代码中添加了 responseJSON 而不是 temp。
猜你喜欢
  • 2021-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多