android - 如何从 Soap Web 服务填充微调器中的所有 json 数组?

标签 android json web-services spinner

下面是我的 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 我还想在零位置显示默认的硬编码文本,如“选择”。谁能解决这个问题?

最佳答案

在下面更改你的 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);
             }                 
       }

关于android - 如何从 Soap Web 服务填充微调器中的所有 json 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38202199/

相关文章:

android - 在 React Native 上缓存图像

与谷歌的 firebase token 生成器一起使用的 Android 唯一设备标识符

android - 我有一个包含多个 ListView 的 ViewPager,我在哪里监听被单击的项目?

ruby - 如何在 Ruby 1.9 中将 Hash 转换为 JSON 字符串?

WCF Web 服务 xml 序列化 [XmlAttributeAttribute()] 被忽略

java - 使用 SOAP WS 进行 ESB 和 Java 应用程序通信

android - init.gradle 文件的用途是什么

javascript - 在 ajax 响应中循环遍历 json 对象

json - JSON 可以安全地用作命令行参数还是需要先进行清理?

web-services - 如何执行 Web 服务和 WCF 的测试?