Note that there are some explanatory texts on larger screens.

plurals
  1. POError converting a HTTP POST response to JSON
    primarykey
    data
    text
    <p>When I try to convert an HTTP POST response to JSONArray I get the error:</p> <p><em>org.json.JSONException: Value of type java.lang.String cannot be converted to JSONArray</em></p> <p>the error happens in the line: <em>JSONArray jArray = new JSONArray(result);</em></p> <p>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.</p> <p>A GET request works just fine, but I need to do a POST request.</p> <p>This is the code:</p> <pre><code>HttpPost("usuarioLogin.php",nameValuePairs); String result = ConvertResponseToString(); try{ JSONArray jArray = new JSONArray(result); JSONObject json_data=null; for(int i=0;i&lt;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(); } </code></pre> <p>this is the code of the function HttpPost()</p> <pre><code>private void HttpPost(String php, ArrayList&lt;NameValuePair&gt; 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()); } } </code></pre> <p>this is the code of the function ConvertResponseToString()</p> <pre><code>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; } </code></pre> <p>this is the code of my php that replies to the POST</p> <pre><code>&lt;?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(); ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload