0

Hi am new to JSON and I will like to retrieve the values for the json below

{"error":"0","uid":"160","langId":"2","rank":"1"}

I wrote a JSONParser class, but the main activity class has got me stuck. Below is the code

private static final String LOGTAG = "MyActivity";
    private static String JSONurl = "http://www.afroklore.org/mobile/boot.php?actn=post_prov&lang=1&uid=2&prov=";
    private static final String ERROR = "error";
    private static final String UID = "uid";
    private static final String LANGID = "langId";
    private static final String RANK = "rank";
    JSONArray output = null;
    String id, error, langId, rank;

if (MenuActivity.status == 2) {
            JSONParser jParser = new JSONParser();
            JSONObject json = jParser.getJSONFromUrl(JSONurl);

            try {
                output = json.getJSONArray(ERROR);

                for (int i = 0; i < output.length(); i++) {
                    JSONObject c = output.getJSONObject(i);

                    // Storing each json item in variable
                    id = c.getString(UID);
                    error = c.getString(ERROR);
                    langId = c.getString(LANGID);
                    rank = c.getString(RANK);
                    // creating new HashMap
                    /*
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(ERROR, error);
                    map.put(UID, id);
                    map.put(LANGID, langId);
                    map.put(RANK, rank);

                    // adding HashList to ArrayList
                    outputField.add(map);*/
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
        return result;

protected void onPostExecute(String r) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            String msg = "Login successful";

            if (MenuActivity.status == 2) {
                Log.d(LOGTAG, result);
                // Log.d(LOGTAG, outputField.get(ERROR));
                Log.d(LOGTAG, error + " " + id + " " + langId + " " + rank);

}

4

3 Answers 3

1

Check your json webservice respose here.it's containing only one json object as string provided by you

{"error":"0","uid":"160","langId":"2","rank":"1"}

so parse it as

JSONObject json = jParser.getJSONFromUrl(JSONurl);
     try {
         // Storing each json item in variable
         id = json.getString(UID);
         error = json.getString(ERROR);
         langId = json.getString(LANGID);
         rank = json.getString(RANK);
           //..your code here
Sign up to request clarification or add additional context in comments.

5 Comments

Hi,it still returned null values
@nnanna : this is afroklore.org/mobile/… complete url where you are making post request?
because when i'm trying to check this url in browser then webservice only returning {"error":"1","error_msg":"Empty data posted"} data
{"error":"1","error_msg":"Empty data posted"} implies invalid username or password You would need to be signed up to get the response as written by me
@nnanna : use Log.d(LOGTAG, error + " " + id + " " + langId + " " + rank); in doInBackground and check you are getting data or not
1

you can use GSON for parsing the JSON strings

In your case create a class that represents the JSON parameters as below

public class Header {
///{"error":"0","uid":"160","langId":"2","rank":"1"}

    private String error;
    private String uid;
    private String langId;
    private String rank;
    /**
     * Gets the error.
     * 
     * @return <tt> the error.</tt>
     */
    public String getError() {
        return error;
    }
    /**
     * Sets the error.
     *
     * @param error <tt> the error to set.</tt>
     */
    public void setError(String error) {
        this.error = error;
    }
    /**
     * Gets the uid.
     * 
     * @return <tt> the uid.</tt>
     */
    public String getUid() {
        return uid;
    }
    /**
     * Sets the uid.
     *
     * @param uid <tt> the uid to set.</tt>
     */
    public void setUid(String uid) {
        this.uid = uid;
    }
    /**
     * Gets the langId.
     * 
     * @return <tt> the langId.</tt>
     */
    public String getLangId() {
        return langId;
    }
    /**
     * Sets the langId.
     *
     * @param langId <tt> the langId to set.</tt>
     */
    public void setLangId(String langId) {
        this.langId = langId;
    }
    /**
     * Gets the rank.
     * 
     * @return <tt> the rank.</tt>
     */
    public String getRank() {
        return rank;
    }
    /**
     * Sets the rank.
     *
     * @param rank <tt> the rank to set.</tt>
     */
    public void setRank(String rank) {
        this.rank = rank;
    }
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Header [error=" + error + ", uid=" + uid + ", langId=" + langId
                + ", rank=" + rank + "]";
    }




}

Now parse the required JSON string in just two lines

String jSonData = "{\"error\":\"0\",\"uid\":\"160\",\"langId\":\"2\",\"rank\":\"1\"}";
Header header = new Gson().fromJson(jSonData, Header.class);
System.out.println("Header : "+header);

You would need gson jar binary for this code to run.

  • Download GSON JAR
  • create a resource folder named 'libs' in your project folder
  • copy the gson jar to this folder and add to build path
  • now you are good to go

Comments

0

{ , } is represent JSONobject and [ ,] is represent JSONArray

{"error":"0","uid":"160","langId":"2","rank":"1"}

You can not take for loop for the JSONobject ,It use for the JSONArray.

You can check this Link,

Here question is difference but use the JSON in that solution.

For more understanding check this Link. I hope it help you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.