Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9069637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:28:54+00:00 2026-06-16T17:28:54+00:00

I’m trying to send JSON data from a php script to an Android app

  • 0

I’m trying to send JSON data from a php script to an Android app and the php script’s output is different from what the Java app expects.

$data['sample']['txt']="hello world";
echo json_encode($data) // {"sample":{"txt":"hello world"}}
//above is incorrect, need {sample : [{txt:"hello world"}]}

The incorrect format results in the following Java exception:

org.json.JSONException: Value {"txt":"hello world"} at sample of type org.json.JSONObject cannot be converted to JSONArray.  

Is there an argument of PHP’s json_encode that I’m missing, or an alternative that would encode it properly?

Java code for async task:

public class RetrieveData extends AsyncTask<List<? extends NameValuePair>, Integer, List<String>> {

    protected List<String> doInBackground(List<? extends NameValuePair>... postData) {
        InputStream is = null;
        List<String> result = new ArrayList<String>();

        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://192.168.72.2:10088/droid_test/test.php");
            httppost.setEntity(new UrlEncodedFormEntity(postData[0]));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
            }
            reader.close();
            is.close();

            JSONObject JSONobj=new JSONObject(sb.toString());
            JSONArray JSONarr=JSONobj.getJSONArray("sample");

            for(int i = 0 ; i < JSONarr.length() ; i++){
                result.add(JSONarr.getJSONObject(i).getString("txt"));
            }

            }
        catch(Exception e) {
            result.add("ERROR "+e.toString());
        }

        return result;
    }

    protected void onPostExecute(List<String> result) {
            getHTTP(result);
    }

}

getHTTP(result) just sets the value to a TextView, which is where the error is displaying. (or the response if I hard code the echo statement)

Solution:

JSONObject JSONobj=new JSONObject(sb.toString());
JSONObject JSONarr=JSONobj.getJSONObject("sample"); // made object per @digitaljoel's suggestion

for(int i=0; i<JSONarr.length(); i++) {
    result.add(JSONarr.getString("txt")); // getting a String, not another array/object *duh*
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T17:28:55+00:00Added an answer on June 16, 2026 at 5:28 pm

    Both of the JSON examples you show are valid JSON, it’s just a matter of what your mapping is on each end.

    Your java code is expecting “sample” to contain a collection (list or array) of objects where each object has a txt field. That’s why it has the [] around the object value in the JSON.

    You can change the mapping on the java side to expect only a single value for sample, or you can change the php code so $data[‘sample’] is an array with a single element that has ‘txt’ = “hello world”.

    If you include the mapping on the java side I can help with that. I’m sure some php guru could help if you want to fix it on the php side.

    Edit:

    JSONArray JSONarr=JSONobj.getJSONArray("sample"); is asking for an array. Change that to a JSONObject and you should be good to go.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am using jsonparser to parse data and images obtained from json response. When
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.