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 8093625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:32:29+00:00 2026-06-05T20:32:29+00:00

I am doing an HttpPost to get data from a php server using async

  • 0

I am doing an HttpPost to get data from a php server using async task. Basically the php script will either return a JSON array or null. It works fine when the json array is returned, however if the script returns null my if statement is not being picked up on and I am being returned this error:

Error parsing data org.json.JSONException: Value null of type org.json.JSONObject$1 cannot be converted to JSONArray

This is a snippet of my script:

    @Override
        protected Void doInBackground(String... params) {
            String url_select = "http://localhost/test.php";
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url_select);
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
        nameValuePairs.add(new BasicNameValuePair("id", id));
            try {
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                //read content
                is =  httpEntity.getContent();  

            } catch (Exception e) {
                Log.e("log_tag", "Error in http connection "+e.toString());
                }

            try {
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                StringBuilder sb = new StringBuilder();
                String line = "";
                    while((line=br.readLine())!=null){
                        sb.append(line+"\n");
                    }
                is.close();
                result=sb.toString();
            } catch (Exception e) {
                Log.e("log_tag", "Error converting result "+e.toString());
            }
            return null;

        }

        protected void onPostExecute(Void v) {

        if(result == "null"){
         this.progressDialog.dismiss();
             startActivity(new Intent(viewRandom.this, allDone.class));
        }else{

        try {
            JSONArray Jarray = new JSONArray(result);
            for(int i=0;i<Jarray.length();i++){
                JSONObject Jasonobject = null;
                Jasonobject = Jarray.getJSONObject(i);
                String id = Jasonobject.getString("id");
        }
            this.progressDialog.dismiss();

        } catch (Exception e) {
            Log.e("log_tag", "Error parsing data "+e.toString());
        }
        }
}
  • 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-05T20:32:30+00:00Added an answer on June 5, 2026 at 8:32 pm

    Change if(result == "null") to if(result == null).

    If you want to check for the string "null" do it with .equals(): if ("null".equals(result))

    I am not sure if you really send a “null” string back from your server but anyway. As you might end returning null (not the string!), you should check for that, too.

    Edit: Why is "null".equals(result) better than result.equals("null")? The answer is: the first one is null-safe which means it will not throw a NullPointerException when result is null. The second one will result in an exception in that case.

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

Sidebar

Related Questions

i doing app for uploading image to php server from android and the php
Doing an ajax get request works as expected using the following code: $.ajax({ type:
Passing data from a controller to another controller. this is what i am doing,
i'm trying to parsing data to the php file and retrive data using jsons.It
With ASP.net MVC I'm doing this: [HttpPost] [TransactionFilter] public ActionResult Create(User user) { //
Doing the below will reproduce my problem: New WPF Project Add ListView Name the
Doing code analysis of the project and get the message Reference-counted object is used
I am trying to post back some data using a viewmodel i have created
I'm having problems getting the new data from the RandomNumbers List I got this
I'm having trouble posting json to a little node.js http server. Post data always

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.