cenobia cenobia - 2 months ago 7
SQL Question

Value [string] <br> [string] <br> of type java.lang.String cannot be converted to JSONArray

I am making an app wherein I fetch user data from the corresponding sql table using php.

<?php

$email = $_POST["email"];

@mysql_connect("localhost","root","root") or die(@mysql_error());
@mysql_select_db("dtbse") or die(@mysql_error());

$x = mysql_query("select * from dtbse where email = '$email' ") or die(@mysql_error());
$result = array();

while ($y=mysql_fetch_array($x)) {
echo $y["uname"]."<br>";
echo $y["gender"]."<br>";
echo $y["pass"]."<br>";
echo $y["address"]."<br>";
echo $y["email"]."<br>";

}

?>


Any help will be greatly apprecitated. I know this question has been a lot of times but I dont think there is something replicating this issue. Thanks.

Here is the code snippet responsible for fetching and parsing.
final ArrayList arr = new ArrayList();
arr.add(new BasicNameValuePair("email", uname));

try {

DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://xxyoxx.esy.es/getDetails.php");
httppost.setEntity(new UrlEncodedFormEntity(arr));
HttpResponse hr = httpclient.execute(httppost);
HttpEntity ent = hr.getEntity();
is = ent.getContent();
Toast.makeText(getApplicationContext(),"1 wrk ",Toast.LENGTH_LONG).show();


} catch (Exception fl) {
Toast.makeText(getApplicationContext(),"First Try error "+fl,Toast.LENGTH_LONG).show();

}
/*// Depends on your web service
httppost.setHeader("Content-type", "application/json");*/

String result=null;
try {
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
result = sb.toString();
Toast.makeText(getApplicationContext(),"2 str\n "+result,Toast.LENGTH_LONG).show();
} catch (Exception sl) {
sl.printStackTrace();

Toast.makeText(getApplicationContext(),"2 err\n "+sl,Toast.LENGTH_LONG).show();
}

try{
String aa = "", b = "", c = "";
JSONArray ar = new JSONArray(result);
for (int i = 0; i < ar.length(); i++) {
JSONObject jo = ar.getJSONObject(i);
aa = jo.getString("uname");
b = jo.getString("address");
c = jo.getString("email");
}
nm.setText(aa);
addr.setText(b);
mail.setText(c);
Toast.makeText(getApplicationContext(),"3 wrk"+result,Toast.LENGTH_LONG).show();
}
catch (Exception tl){
Toast.makeText(getApplicationContext(),"3 err "+tl,Toast.LENGTH_LONG).show();
}

Answer

Strings separated by <br> are not a valid JSON array. PHP can create JSON strings using json_encode

If you need to read a JSON array in Android you need to echo a JSON array from PHP:

<?php 

$email = $_POST["email"];

mysql_connect("localhost","root","root") or die(mysql_error());
mysql_select_db("dtbse") or die(mysql_error());

$x = mysql_query("select * from dtbse where email = '$email' ") or die(mysql_error());
$result = array();

$res=[];    
while ($y=mysql_fetch_array($x)) {
     $res[] = [ 
              $y["uname"], 
              $y["gender"],                   
              $y["pass"], 
              $y["address"],
              $y["email"] 
     ];    
 }
 echo json_encode($res); //Make PHP return a valid JSON response

Also, the error suppression operators may hide valuable debug infomation which may help you diagnose other problems.

If you instead prefer to pass the JSON object to Java then you can do the following (simpler) thing.

<?php 

$email = $_POST["email"];

mysql_connect("localhost","root","root") or die(mysql_error());
mysql_select_db("dtbse") or die(mysql_error());

$x = mysql_query("select * from dtbse where email = '$email' ") or die(mysql_error());
$result = array();

$res=[];    
while ($y=mysql_fetch_array($x)) {
     $res[] = $y;
 }
 echo json_encode($res); //Make PHP return a valid JSON response