2

I am try to get a group of data from php server to android. but a error are occurred when I execute the project, try refer to several related questions however I still cant come up with a solution.

here is my asynctask which will establish connection and sent momentID to php

class GetAllComment extends AsyncTask<String, String, ArrayList<Comment>> {


    protected ArrayList<Comment> doInBackground(String... params) {
                    List<NameValuePair> param = new ArrayList<NameValuePair>();
                    int success;
        try {
            Log.d("","@@@### " + postedID);
            param.add(new BasicNameValuePair("momentid", postedID));
            JSONObject json = jsonParser.makeHttpRequest(
                    "http://192.168.168.111:80/testmoment/getallcomment.php", "GET", param);

            // check your log for json response
            Log.d("Single comments Details", json.toString());

            // json success tag
            success = json.getInt("success");
            if (success == 1) {

                JSONArray productObj = json
                        .getJSONArray("product"); // JSON Array


                try {
                    JSONObject comments = productObj.getJSONObject(0);
                    Comment comment = new Comment();
                    comment.setCommentContentbody(comments.getString("vcontentbody"));
                    comment.setCommentDate(comments.getString("vcdate"));
                    comment.setCommentTime(comments.getString("vctime"));
                    list.add(comment);

                    Log.d("","tryget : " + comments.getString("vcid") + " AND " + comments.getString("vcontentbody") + " AND " + comments.getString("vcdate") + " AND " + comments.getString("vctime"));

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            } else {

            }
        }catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }

    protected void onPostExecute(ArrayList<Comment> list) {
        cAdapter = new CommentAdapter(getActivity().getBaseContext(), mCommentLists);
        commentList.setAdapter(cAdapter);
        mComment = new Comment();
        cAdapter.Update();

        if(list != null){
            for(Comment comment: list){
                cAdapter.add(comment);
            }
        }


        cAdapter.notifyDataSetChanged();
    }
}

here is my php script :

<?php

header('Content-type: application/json');
include "db.config.php";
if (isset($_GET['momentid'])){
$momentid = $_GET['momentid'];
$result =mysql_query("SELECT * FROM commenttable WHERE vcmomentid = $momentid");

if (!empty($result)) {
    // check for empty result
    while($row=mysql_fetch_assoc($result))
        $json_output[]=$row;

print(json_encode($json_output));

mysql_close();
        echo json_encode($response);
    } else {
        // no product found
        $response["success"] = 0;
        $response["message"] = "No product found";

        // echo no users JSON
        echo json_encode($response);
    }
} else {
    // no product found
    $response["success"] = 0;
    $response["message"] = "No product found";

    // echo no users JSON
    echo json_encode($response);
}

?>

here is the error I get once execute :

JSON Parser: Error parsing data org.json.JSONException: Value [{"vcontentbody":"gkkgv","vcid":"1","vcmomentid":"109","vcdate":"2015-11-23","vcuserid":"1578","vctime":"17:09 PM"}] of type org.json.JSONArray cannot be converted to JSONObject

Please Help me find the error. Thanks

4
  • 2
    your data is retrieve in JSON Array type not JSON Object type. so get data in JSON Array not JSON Object Commented Nov 24, 2015 at 7:13
  • 2
    As in log of type org.json.JSONArray cannot be converted to JSONObject Commented Nov 24, 2015 at 7:14
  • Post your Json data. Commented Nov 24, 2015 at 7:14
  • edit the code please? I know my data are retrieve in JsonArray but not Jsonobject, but my problem is how to change the Jsonobject to JsonArray? Commented Nov 24, 2015 at 7:17

2 Answers 2

2

Actually you are getting JSONArray that you are converting to JSONObject

[ // This is Array
    { // This is Object
    "vcontentbody":"gkkgv",
    "vcid":"1",
    "vcmomentid":"109",
    "vcdate":"2015-11-23",
    "vcuserid":"1578",
    "vctime":"17:09 PM"
    }
]

So you have to write like:

JSONArray jsonArray = jsonParser.makeHttpRequest("http://192.168.168.111:80/testmoment/getallcomment.php", "GET", param);

Now check whether it is null or not

if(jsonArray != null)
{
    // Here You may not have any key like 'success' so i don't have taken.

    // You can directly get JSON Object here.. If you are getting more than one JSONObject then make loop here...
    JSONObject object = jsonArray.getJSONObject(0);

    // Now you can do with object that you want to do.
    Comment comment = new Comment();
    comment.setCommentContentbody(object.getString("vcontentbody"));
    comment.setCommentDate(object.getString("vcdate"));
    comment.setCommentTime(object.getString("vctime"));
    list.add(comment);
}

If any question, You can ping me. May it will helpful to you. Thanks.

Sign up to request clarification or add additional context in comments.

Comments

1

Here is the code by which you can parse your data ,please put your jsonArray in response variable

try {
        JSONArray arr=new JSONArray(response);

        JSONObject jobj=new JSONObject(arr.get(0).toString());

        for(int i=0;i<jobj.length();i++)
        {
           Log.e("Index 0", ":"+jobj.getString("vcontentbody")) ;
           Log.e("Index 1", ":"+jobj.getString("vcid"));
           Log.e("Index 2", ":"+jobj.getString("vcmomentid")) ;
           Log.e("Index 3", ":"+jobj.getString("vcuserid")) ;
           Log.e("Index 4", ":"+jobj.getString("vcdate")) ;
           Log.e("Index 4", ":"+jobj.getString("vctime")) ;
        }


    } catch (JSONException e) {
        e.printStackTrace();
    }

3 Comments

i am not good in parse the json i just want to ask you that you are using arr.get(0) what exactly is that
we are doing that for getting the first position
i am doing that for getting first array

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.