0

我使用 ksoap webservice 访问数据,如果只有数据不存在大括号,我会得到结果。但是在 web 服务中,数据在大括号{} 中可用,在这种情况下,我无法访问数据,我只得到空数据,它看起来像这样,

{“消息”:“需要处理贷款类型。”}

这是一个 ksoap webservice 输出,现在我如何访问大括号内的值,请任何人帮助我。

4

3 回答 3

1

好的..

SoapObject response = (SoapObject)envelope.getResponse();
String rate =  response.getProperty(0).toString();
String pi =  response.getProperty(1).toString();
String apr = response.getProperty(2).toString();
txtbox.setText = rate + " " + pi + " " + apr;
于 2012-07-30T10:38:58.583 回答
1

好的..您的 Web 服务在响应中返回 JSON 数据,以便您在代码中解析 Json 数据,我已经给出了一些基本逻辑如何解析 JSON Web 服务,

//=================================================

    private ArrayList<HashMap<String, String>> MyArray = new ArrayList<HashMap<String,String>>();
    private HashMap<String, String> MyTempHas;

    private JSONObject JSONObj;
    Private SoapObject responce = (SoapObject) envelope.getResponse();
    /* gets our result in JSON String */
    private String ResultObject = responce.getProperty(0).toString();

    if (ResultObject.startsWith("{")) { // if JSON string is an object
        JSONObj = new JSONObject(ResultObject);
        Iterator<String> itr = JSONObj.keys();
        MyTempHas = new HashMap<String, String>();
        while (itr.hasNext()) {
            String Key = (String) itr.next();
            String Value = JSONObj.getString(Key);
            MyTempHas.put(Key, Value);
            // System.out.println(bundleResult.getString(Key));
        }
        MyArray.add(MyTempHas);
    } else if (ResultObject.startsWith("[")) { // if JSON string is an array
        JSONArr = new JSONArray(ResultObject);
        System.out.println("length" + JSONArr.length());
        for (int i = 0; i < JSONArr.length(); i++) {
            JSONObj = (JSONObject) JSONArr.get(i);
        Iterator<String> itr = JSONObj.keys();
            MyTempHas = new HashMap<String, String>();
            while (itr.hasNext()) {
                String Key = (String) itr.next();
                String Value = JSONObj.getString(Key);
                MyTempHas.put(Key, Value);
                // System.out.println(bundleResult.getString(Key));
            }
            MyArray.add(MyTempHas);
        } 
    }

//==========================================

在此示例中,如果您只返回一个对象,则它以“{”开头并将所有属性保存在 Hasmap 中,然后添加到 ArrayList 中。如果您的响应发送多个对象,则它以“[”数组开头,然后按照上述逻辑逐个解析对象。

然后简单地从存储在 ArrayList 中的 Hasmap 中获取所有值。如果您无法从 ArrayList 中获取价值,请告诉我我会帮助您。

我希望这能帮到您。

于 2012-07-30T11:53:10.997 回答
0

试试这个示例代码 :: 它会帮助你 ::

public class Mywebservice1Activity extends Activity implements OnClickListener {

EditText edtuname, edtpwd;
Button btnok, btncancel;

private static final String namespace = "http://tempuri.org/";
private static final String address = "http://192.168.1.138/test/Service.asmx";
private static final String method = "login1";
private static final String action = namespace + method;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    edtuname = (EditText) findViewById(R.id.edtname);
    edtpwd = (EditText) findViewById(R.id.edtpwd);

    btnok = (Button) findViewById(R.id.btnok);
    btncancel = (Button) findViewById(R.id.btncancel);

    btnok.setOnClickListener(this);
    btncancel.setOnClickListener(this);

}

@Override
public void onClick(View v) {

    if (v == btnok) {

        String name = edtuname.getText().toString();
        String pass = edtpwd.getText().toString();

        SoapObject request = new SoapObject(namespace, method);
        SoapSerializationEnvelope envelop = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);

        request.addProperty("user", name.toString().trim());
        request.addProperty("pass", pass.toString().trim());

        envelop.setOutputSoapObject(request);
        envelop.dotNet = true;

        HttpTransportSE transport = new HttpTransportSE(address);

        try {
            transport.call(action, envelop);
            Object res = envelop.getResponse();

            Toast.makeText(this, res.toString(), Toast.LENGTH_SHORT).show();

            // txt.setText(res.toString());

        } catch (Exception e) {
            // txt.setText(e.toString());
        }

    }

}

}

于 2012-07-30T10:15:45.363 回答