如何从jsonObject获取变量中的值?

时间:2016-07-02 07:17:07

标签: android web-services soap

我的Soap Web服务中的响应如下:

    [{"msg":"1,UDDAIPUR","Value1":null,"Value2":null,"Value3":null,"Value4":null,"Value5":null,"Value6":null,"Value7":null,"Value8":null,"Value9":null,"Value10":null}]

下面是我的AsyncTask -

//String TAG_IDS And TAG_NAMES
private static final String TAG_ID_fillstateid = "nSerialNo";

//TO call web service for City
class async_fillCity extends AsyncTask<String, Void, String> {

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

        SoapObject request = new SoapObject(namespace, method_name__fillcity);
        request.addProperty(parameter_fillcity, "1");//add the parameters

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);//set soap version
        envelope.setOutputSoapObject(request);
        envelope.dotNet = true;
        try {
            HttpTransportSE androidHttpTransport = new HttpTransportSE(url);
            androidHttpTransport.call(soap_action_fillcity, 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();
            resultcity = response.getPropertyAsString(0).toString();

            if (resultcity != null) {
                try {

                    JSONArray resultarray = new JSONArray(resultcity);
                    for (int i = 0; i < resultarray.length(); i++) {
                        JSONObject c = resultarray.getJSONObject(i);
                        String idCity = c.getString(TAG_ID_fillstateid);
                        String nameCity = (c.getString(TAG_Name_fillstatename));
                        HashMap<String, String> hashstate = new HashMap<String, String>();
                        hashstate.put(TAG_ID_fillstateid, idCity);
                        hashstate.put(TAG_Name_fillstatename, nameCity);
                        resultListstate.add(hashstate);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }

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

    }

    @Override
    protected void onPostExecute(String result1) {
        // TODO Auto-generated method stub
        final SpinnerAdapter adapter = new SimpleAdapter(MainActivity.this, resultListstate, R.layout.activity_showspinner_state, new String[]{TAG_ID_fillstateid, TAG_Name_fillstatename}, new int[]{R.id.textidstate, R.id.textnamestate});

        city.setAdapter(adapter);

        city.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
                // TODO Auto-generated method stub

                TextView txtid = (TextView) findViewById(R.id.textidstate);
                City = txtid.getText().toString();
                // Call web service for district
                //calldistt();
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
            }
        });
        super.onPostExecute(result1);

    }
}

我收到错误:

  

&#34; org.json.JSONException:nSerialNo没有值&#34;

1 个答案:

答案 0 :(得分:0)

你可以像这样分别获得id和city -

                JSONArray resultarray = new JSONArray(resultcity);
                for (int i = 0; i < resultarray.length(); i++) {
                    JSONObject c = resultarray.getJSONObject(i);
                   String idCity = c.getString(msg);}

现在你在idCity字符串中获得城市ID和城市名称,现在你必须按照这样的方式拆分字符串(,)

String[] namesList = idCity.split(",");
String id = namesList[0];
String cityName = namesList[1];

幸福快乐......