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

  • Home
  • SEARCH
  • 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 7744695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:49:45+00:00 2026-06-01T09:49:45+00:00

When I try to convert an HTTP POST response to JSONArray I get the

  • 0

When I try to convert an HTTP POST response to JSONArray I get the error:

org.json.JSONException: Value of type java.lang.String cannot be converted to JSONArray

the error happens in the line: JSONArray jArray = new JSONArray(result);

the value of the string result is [{“return”:”1″}] but it includes an extra blank character at the beginning that when removed, solves the problem. However, this character is not blank because a trim does not solve the problem. I believe there is some problem with the POST response, maybe badly constructed? (or maybe the POST request is wrong?) Any help is welcome.

A GET request works just fine, but I need to do a POST request.

This is the code:

HttpPost("usuarioLogin.php",nameValuePairs);
String result = ConvertResponseToString();
try{

    JSONArray jArray = new JSONArray(result);
    JSONObject json_data=null;
    for(int i=0;i<jArray.length();i++){
        json_data = jArray.getJSONObject(i);                
        ret = json_data.getInt("return");   
            retorno = (ret==1)?true:false;
    }               

}
catch(JSONException e1){          
      e1.printStackTrace();       
} catch (ParseException e1) {
    e1.printStackTrace();
}       

this is the code of the function HttpPost()

private void HttpPost(String php, ArrayList<NameValuePair> nameValuePairs)
{
            try{
                  HttpClient httpclient = new DefaultHttpClient();
                  String host = com.android.taggies.LoginUser.getContext().getResources().getString(R.string.host);
                  HttpPost httppost = new HttpPost("http://"+host+php);
                  httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                  HttpResponse response = httpclient.execute(httppost);
                  HttpEntity entity = response.getEntity();
                  is = entity.getContent();
            }catch(Exception e){
                  Log.e("log_tag", "Error in http connection "+e.toString());
            }
}

this is the code of the function ConvertResponseToString()

private String ConvertResponseToString()
{
    //convert response to string
    String result = null;
    try{
        //BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"));
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();

        result=sb.toString();
    }
    catch(Exception e){
        Log.e("log_tag", "Error converting result "+e.toString());
    }

    return result;
}

this is the code of my php that replies to the POST

<?php
mysql_connect("localhost","root","");
mysql_select_db("dbTaggies");

$q=mysql_query("SELECT count(*) as 'return' FROM users
WHERE name='$_POST[user]' AND password ='$_POST[pass]'");

while($e=mysql_fetch_assoc($q))
{
        $output[]=$e;
}       

print(json_encode($output));

mysql_close();
?>
  • 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-01T09:49:46+00:00Added an answer on June 1, 2026 at 9:49 am

    The problem is solved.

    PHP files were saved in UTF-8 WITH BOM, the solution was saving the files in UTF8 no BOM and the initial character in the POST response was removed.

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

Sidebar

Related Questions

In Java you can try to convert any String value to Integer, but when
We try to convert from string to Byte[] using the following Java code: String
Try loading this normal .jpg file in Internet Explorer 6.0. I get an error
An user can post multiple comments in a thread, and I try to get
I want to try to convert a string to a Guid, but I don't
When I try to convert between date and string in Flash CS4, I use
I'm using Lyx to produce a Latex document, and when i try to convert
I've used the 'hg convert' command to try to move my SVN repository to
I really try to find on google how can we convert .doc and .txt
I am trying to get the post get method in the android app, 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.