0

Im using ksoap webservice to access data and iam getting result if only data present without braces. But in webservices the data is available inside braces{}, in that case i cant access the data and i get only null data, and it looks like this,

{"Message":"Loan Type is Required to Process."}

this was an ksoap webservice output, and now how can i access values inside braces, please anyone help me.

Vishnu
  • 349
  • 3
  • 8
  • 16
  • Explain More Detail of your Problem – Gautam Vasoya Jul 30 '12 at 10:10
  • Hello sir, Iam trying to access .net webservice in android using ksoap, and i have succeeded in accessing that and im getting an value. if .net webservice returls like this, 4:13 AM then im getting an value 4:13, but if returns like this {"30_year_rate":4.250,"30_year_pi":196.78,"30_year_apr":4.375,"QuoteID":1432,"Licensed":"N"} , i enable to get the values that was inside the braces... and this was my problem. – Vishnu Jul 30 '12 at 10:15

3 Answers3

1

Ok..

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;
Gautam Vasoya
  • 881
  • 1
  • 8
  • 16
1

Ok.. Your Web Service Return JSON Data in Responce, so that you have Parse the Json Data in your Code, i Have Give Some Basic Logic How to Parse JSon Web Service,

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

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

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

In This Example if you have return only One Object then it Start with "{" and save all the atribute in Hasmap and then add into ArrayList. If your Responce send Multiple Object then it Start with "[" array , then parse one by one object as per above logic.

then simle you get all the value from Hasmap which is store in ArrayList. If you not able to get Value from the ArrayList then Tell me I will Help you.

I Hope this will Help you.

Gautam Vasoya
  • 881
  • 1
  • 8
  • 16
0

Try this Sample Code :: May it will Help you ::

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());
        }

    }

}

}

Gautam Vasoya
  • 881
  • 1
  • 8
  • 16
  • im ok with parsing the data, but my problem is how to access the values that reside inside the braces like this {"30_year_rate":4.250,"30_year_pi":196.78,"30_year_apr":4.375,"QuoteID":1432,"Licensed":"N"} and this was an XML output and now i want to access the value that are in inside the braces. – Vishnu Jul 30 '12 at 10:20
  • You Get Responce in Toast Printing?? – Gautam Vasoya Jul 30 '12 at 10:29
  • sir, if the value is inside the braces then its not printing for eg " {"30_year_rate":4.250,"30_year_pi":196.78,"30_year_apr":4.375,"QuoteID":‌​1432,"Licensed":"N"}" this i enable to get using getproperty(0), but if the value comes without braces like Android , in this i able to get the value "Android"... – Vishnu Jul 30 '12 at 11:06