0

I've follow the code for parsing the value with JSON from here, but I get the problem in my return statement. I want to put the parsing result into my return statement. How to do that?


Here is my code:

public String MASUK(String user, String password)
    {
        SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);

        PropertyInfo pi = new PropertyInfo();
        pi.setName("ccduser");
        pi.setValue(user);
        pi.setType(String.class);
        request.addProperty(pi);

        PropertyInfo pi2 = new PropertyInfo();
        pi2.setName("password");
        pi2.setValue(password);
        pi2.setType(String.class);
        request.addProperty(pi2);


        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;

        envelope.setOutputSoapObject(request);

        HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
        try
        {
            httpTransport.call(SOAP_ACTION, envelope);
            SoapObject resultSOAP = (SoapObject) envelope.bodyIn;

            /* gets our result in JSON String */
            String ResultObject = resultSOAP.getProperty(0).toString();

            resultSOAP = (SoapObject) envelope.bodyIn;
            ResultObject = resultSOAP.getProperty(0).toString();

            if (ResultObject.startsWith("{")) { // if JSON string is an object
                JSONObj = new JSONObject(ResultObject);
                Iterator<String> itr = JSONObj.keys();
                while (itr.hasNext()) {
                    String Key = (String) itr.next();
                    String Value = JSONObj.getString(Key);
                    BundleResult.putString(Key, Value);
                    // System.out.println(bundleResult.getString(Key));
                }
            } 

            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);
                    BundleResult.putString(String.valueOf(i), JSONObj.toString());
                    // System.out.println(bundleResult.getString(i));
                } 
            }
        }
        catch (Exception exception)
        {

        }

        return null;

    }
Community
  • 1
  • 1
blankon91
  • 521
  • 3
  • 15
  • 39

3 Answers3

1

If you have JSON array object:

JSONObject jObject = new JSONObject(***JSON String you have ***);
JSONArray contestantObjects = jObject.getJSONArray("*** Array Name ***");
for(int i=0; i<contestantObjects.length(); i++) {
        mChannels.setId(contestantObjects.getJSONObject(i).getString("id").toString());
        mChannels.setName(contestantObjects.getJSONObject(i).getString("name").toString());
}

If you have just one item:

JSONObject jObject = new JSONObject(***JSON String you have ***);
mPreview.setBody(jObject.getString("*** Item Name ***").toString());
mPreview.setPublishedDate(jObject.getString("publishedDate").toString());
mPreview.setRefKey(jObject.getString("refKey").toString());
mPreview.setTitle(jObject.getString("title").toString());

http://jsonviewer.stack.hu/ is online tool for parsing JSON. its awesome you can use it.

Hesam
  • 52,260
  • 74
  • 224
  • 365
  • I can't open the site, but I will try this code, but how to put the result from this code into return statement? thank you :) – blankon91 Jun 19 '12 at 02:43
  • Yes, i checked it, it seems their server is down currently. you can try to open it later. 1) you need to create a class based on your JSON format. 2) create an object from that class (in my above example mChannels and mPreview) are object of that class. 3) as mentioned in above, add json parameters to objects. 4) returning this object. – Hesam Jun 19 '12 at 02:50
  • if it didn't help you. tell me to add some more code to clarify this process for you. – Hesam Jun 19 '12 at 02:51
  • I've error redline on myPreview, here is my code: `httpTransport.call(SOAP_ACTION, envelope); SoapObject resultSOAP = (SoapObject) envelope.bodyIn; String ResultObject = resultSOAP.getProperty(0).toString(); resultSOAP = (SoapObject) envelope.bodyIn; ResultObject = resultSOAP.getProperty(0).toString(); // if JSON string is an object JSONObject jObject = new JSONObject(ResultObject); mPreview.setTitle(jObject.getString("password").toString()); mPreview.setBody(jObject.getString(password).toString());` is there something wrong? – blankon91 Jun 19 '12 at 03:41
  • I can speak just about last three lines, for rest i have no idea what you are doing. Can you put your feed address here? i need to see what is your JSON String (or if you don't have just share your JSON string). Did you create a class for object mPreview? because i see you are using my suggestion but i don't know did you create a class for this object. Also, there is something wrong in last line. "password" needs to be surrounded by "". Also, if you do this last two lines becomes similar to each other. Is it correct? – Hesam Jun 19 '12 at 04:36
  • here is the JSON String `GetUserResponse{GetUserResult=anyType{ccduser=DARMANTO ; password=123456; cnmuser=Darmanto ;};}` I don't know where I should create the class for object mPreview, can you tell me how? :( and for my last 2 lines, I don't know what setTitle and setBody is. what are these? – blankon91 Jun 19 '12 at 04:39
  • First your JSON is invalid. Second, You can simplify my above code to something like: JSONObject jObject = new JSONObject(ResultObject); String password = jObject.getString("password").toString(); String cnmuser = Object.getString("cnmuser").toString(); String ccduser = Object.getString("ccduser").toString(); – Hesam Jun 19 '12 at 04:46
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/12722/discussion-between-blankon91-and-hesam) – blankon91 Jun 19 '12 at 04:47
  • I need to go for lunch :) Please see this link, I explained this process in detail: http://stackoverflow.com/questions/10611659/android-how-do-i-parse-this-mixed-up-json-response/10611840#10611840 – Hesam Jun 19 '12 at 04:53
0
JSONObject prefsJson = new JSONObject(prefsJsonString);
String str = prefsJson.has("str") == true ? prefsJson.getString("str") : "";

Is that ok?

Thiem Nguyen
  • 6,345
  • 7
  • 30
  • 50
jues
  • 1
0

Referring to the link which you have used to parse the JSON String, I would suggest that you return the object bundleResult from your function. Change the return value for your method as public Bundle MASUK(String user, String password).

EDIT

Don't forget to initialize the Bundle object:

private Bundle bundleResult=new Bundle();

And take care not to change the case (upper/lower case) for the variable being used. I could see in your code BundleResult.putString(Key, Value); and the reference link uses bundleResult.putString(Key, Value);

Arun George
  • 18,352
  • 4
  • 28
  • 28
  • but I've problem in my class for calling the web service: `public class Panggil extends Thread{ public CariUser cs; public String user; public String password; public void run(){ try{ cs=new CariUser(); Bundle login = cs.MASUK(user, password); LoginActivity.hasil = login; } catch(Exception ex){ //LoginActivity.hasil = ex.toString(); } } }` it says mismatch `Type mismatch: cannot convert from Bundle to String` – blankon91 Jun 19 '12 at 03:26
  • You are getting this error because you may have declared `LoginActivity.hasil` as `String` whereas `login` is of type `Bundle`. Suggestion - After you return your result as a `Bundle` extract from that object the String values of your choice using the getter methods of Bundle. – Arun George Jun 19 '12 at 03:55
  • how to extract that object? because I'm confusing using `ResourceBundle myResources = ResourceBundle.getBundle("My String", mylocale);` – blankon91 Jun 19 '12 at 04:09