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

Android 如何从jsonObject获取变量中的值?,android,web-services,soap,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}] 下面是我的任务- //String TAG_IDS And TAG_NAMES private static fina

我的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}]
下面是我的任务-

//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);

    }
}
//字符串标记ID和标记名称
私有静态最终字符串标记\u ID\u fillstateid=“nSerialNo”;
//为城市调用web服务
类async\u fillCity扩展了AsyncTask{
@凌驾
受保护的字符串doInBackground(字符串…参数){
SoapObject请求=新的SoapObject(名称空间、方法名称和填充城市);
request.addProperty(参数_fillcity,“1”);//添加参数
SoapSerializationEnvelope=新的SoapSerializationEnvelope(SoapEnvelope.VER11);//设置soap版本
envelope.setOutputSoapObject(请求);
envelope.dotNet=true;
试一试{
HttpTransportSE androidHttpTransport=新的HttpTransportSE(url);
androidHttpTransport.call(soap_action_fillcity,信封);//这是将调用Web服务的实际部分
//SoapPrimitive prim=(SoapPrimitive)envelope.getResponse();//从信封正文获取SoapResult。
SoapObject响应=(SoapObject)envelope.bodyIn;
//resultstate=prim.toString();
resultcity=response.getPropertyAsString(0.toString();
if(resultcity!=null){
试一试{
JSONArray resultarray=新的JSONArray(resultcity);
对于(int i=0;i
我得到一个错误:

“org.json.JSONException:nSerialNo没有值”


你可以像这样把身份证和城市分开-

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

快乐到快乐…

您可以像这样分别获得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);}

Happy to Happy…

在您的json结果中没有类似于“nSerialNo”的值,这就是您出错的原因。JsonTank中没有用于响应的键
nSerialNo
。你能给出我如何在字符串类型的变量中同时获取cityid和cityname的解决方案吗?在json结果中没有类似于“nSerialNo”的值,这就是为什么会出现错误。在JSonTank中没有这样的键
nSerialNo
,用于响应。你们能给我一个解决办法吗?我是如何在字符串类型的变量中同时得到cityid和cityname的?谢谢Shane,你们节省了我的时间。我第一次使用web服务,但又一次被卡住了,因为我想在spinner中填充cityName,但它的给定错误。嗨,Shane,现在我在异常中遇到了这个错误。你能解决这个“java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'boolean java.util.ArrayList.add(java.lang.Object)'吗?”resultListstate.add(hashstate);为null指定一个字符串类型的列表-list cityname=new ArrayList();然后将数据添加到idCity字符串下面的doInBackgroundMethod列表中,如-cityname.add(idCity);然后将setadapter设置为类似-spinner.setadapter的微调器(新的“YourAdapter”(此,);这里的列表名是citynameThanks Shane,但这不是我想要的。我想在TextView中设置cityId和cityName,然后在微调器中仅显示cityName,然后我想为选定城市的本地位置填充另一个微调器我想在HashMap中添加这两个值,然后在onPostExecuteThanks中填充微调器Shane您节省了我的时间。我第一次使用web服务,但又一次被卡住了,因为我想在spinner中填充cityName,但它的给定错误。嗨,Shane,现在我在异常中遇到了这个错误。你能解决这个“java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'boolean java.util.ArrayList.add(java.lang.Object)'吗?”resultListstate.add(hashstate);为null指定一个字符串类型的列表-list cityname=new ArrayList();然后将数据添加到idCity字符串下面的doInBackgroundMethod列表中,如-cityname.add(idCity);然后将setadapter设置为类似-spinner.setadapter的微调器(新的“YourAdapter”(此,);这里的列表名是citynameThanks Shane,但这不是我想要的。我想在TextView中同时设置cityId和cityName,然后在微调器中只显示cityName,然后我想为所选城市的本地位置填充另一个微调器我想在HashMap和