【问题标题】:How to populate all json array in spinner from Soap Web Service?如何从 Soap Web 服务填充微调器中的所有 json 数组?
【发布时间】:2016-07-05 11:22:15
【问题描述】:

以下是我的 JSON 响应

[
  {
    "msg":"4,MADHUVAN,5,AMBAMATA,6,PH-1,7,PH-2",
    "Value1":null,
    "Value2":null,
    "Value3":null,
    "Value4":null,
    "Value5":null,
    "Value6":null,
    "Value7":null,
    "Value8":null,
    "Value9":null,
    "Value10":null
  }
]

下面是我的 Async_Task -

class async_Division extends AsyncTask<String, Void, String> {

  @Override
  protected String doInBackground(String... params) {

     SoapObject request = new SoapObject(namespacedivision, method_name__filldivision);
     request.addProperty(parameter_filldivision, circle);//add the parameters

     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);//set soap version
     envelope.setOutputSoapObject(request);
     envelope.dotNet = true;
     try {
        HttpTransportSE androidHttpTransport = new HttpTransportSE(url_division);
        androidHttpTransport.call(soap_action_filldivision, envelope);  // this is the actual part that will call the webservice
        //SoapPrimitive prim = (SoapPrimitive) envelope.getResponse();  // Get the SoapResult from the envelope body.
        SoapObject response = (SoapObject) envelope.bodyIn;
        //resultstate = prim.toString();
        resultDivision = response.getPropertyAsString(0).toString();

        if (resultDivision != null) {
           try {

              JSONArray resultarray = new JSONArray(resultDivision);
              for (int i = 0; i < resultarray.length(); i++) {
                 JSONObject c = resultarray.getJSONObject(i);

                 String idLocality = c.getString("msg");
                 String nameLocality = (c.getString("msg"));

                 // Split data by comma
                 String[] namesList = idLocality.split(",");
                 String idDivision = namesList[0];
                 String nameDivision = namesList[1];
                 //cityname.add(cityName);

                 HashMap<String, String> hashlocality = new HashMap<String, String>();
                 hashlocality.put(TAG_ID_fillcombodivisionid, idDivision);
                 hashlocality.put(TAG_Name_fillcombodivisionname, nameDivision);
                 resultListDivision.add(hashlocality);
              }
           } catch (JSONException e) {
              e.printStackTrace();
           }
        } else {
           Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

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

  }

  @Override
  protected void onPostExecute(String result1) {

     // TODO Auto-generated method stub

     SpinnerAdapter adapter = new SimpleAdapter(getActivity(), resultListDivision, R.layout.activity_showspinner_division, new String[]{TAG_ID_fillcombodivisionid, TAG_Name_fillcombodivisionname}, new int[]{R.id.textidlocality, R.id.textnamelocality});
       spnDivision.setAdapter(adapter);

     spnDivision.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
           // TODO Auto-generated method stub
           //save.setClickable(true);
           TextView txtid = (TextView) arg0.findViewById(R.id.textidlocality);
           division = txtid.getText().toString();
           // Call web service for district
           //callvillage();
           new async_Sub_Division().execute();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }

     });

  }
  }

我在我的Spinner 中填充上面的 JSON 响应。一切正常,但我的Spinner 只显示一个值“MADHUVAN”。我想在我的Spinner 中显示所有JSONArray,我还想在零位置显示默认的硬编码文本,如“选择”。谁能解决这个问题?

【问题讨论】:

  • 这是因为您的 json 中只有一个值。
  • 感谢 Shane 的回复,但上面我提到了我的 Json 回复和更多价值,例如“MADHUVAN,AMBAMATA”,但它只显示“MADHUVAN”。
  • 那么您的服务器响应中存在一些问题。不在这里
  • 这是因为你用逗号分割字符串,所以只有第一个值来找你。

标签: android json web-services spinner


【解决方案1】:

在下面更改你的 for 循环代码就可以了,

       for (int i = 0; i < resultarray.length(); i++) 
       {
             JSONObject c = resultarray.getJSONObject(i);

             String idLocality = c.getString("msg");

             // Split data by comma
             String[] namesList = idLocality.split(",");

             for(int i = 0; i < nameList.length; i+=2)
             {
                String idDivision = namesList[i];
                String nameDivision = namesList[i + 1];                 

                HashMap<String, String> hashlocality = new HashMap<String, String>();
                hashlocality.put(TAG_ID_fillcombodivisionid, idDivision);
                hashlocality.put(TAG_Name_fillcombodivisionname, nameDivision);
                resultListDivision.add(hashlocality);
             }                 
       }

【讨论】:

  • 感谢 Vickyexpert 它对我有用。我已经提到了另一个问题,我想在零位置显示字符串“选择”默认值。您能对此提出任何建议吗?
  • 你可以在上面的 for 循环中比较 if(i == 0) { hashlocality.put(TAG_ID_fillcombodivisionid, -1); hashlocality.put(TAG_Name_fillcombodivisionname, "Select"); }
  • 当答案对真正正在寻找这个的用户有用时,那么否决答案的含义是什么?
  • 嗨,Vicky,我已在 HashMap 下的 for 循环中申请,但“选择”未显示在零位置。我的数组从 1 开始,所以默认为零位置我想显示 Select
  • 我不知道他们为什么给你减分,因为你节省了我的时间,谢谢。坚持下去
猜你喜欢
  • 2012-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-24
  • 1970-01-01
  • 2013-06-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多