presta
presta

Reputation: 43

org.json.JSONException: End of input at character 0 of

I need to select the data from MySql to Spinner in Android.

I don't know where the error and how can I solved this.

public void selectfromclass(){

    ArrayList<NameValuePair> nameValuePairs = new ArrayList<>();

    String test= cyclespiner.getText().toString();
    Log.d("toooooooooooooooooooooooooooooooop",test);

    nameValuePairs.add(new BasicNameValuePair("name", test));



    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://xxxxx/getclassprof.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
        Log.e("pass 1", "connection success ");
    } catch (Exception e) {
        Log.e("Fail 1", e.toString());
        Toast.makeText(getActivity(), "Invalid IP Address",
                Toast.LENGTH_LONG).show();
    }

    try {
        BufferedReader reader = new BufferedReader
                (new InputStreamReader(is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        while ((line = reader.readLine()) != null) {
       //     sb.append(line + "\n");
        }
        is.close();
        result = sb.toString();
        Log.e("pass 2", "connection success ");
    } catch (Exception e) {
        Log.e("Fail 2", e.toString());
    }




    try {

        JSONObject json=new JSONObject(result);
        JSONArray JA = json.getJSONArray("matier");
       /// JSONObject json = null;
        roll_no = new String[JA.length()];
        name = new String[JA.length()];

        for (int i = 0; i < JA.length(); i++) {
            json = JA.getJSONObject(i);

            name[i] = json.getString("class");
        }


        spinner_fn();

    } catch (Exception e) {

and this is the code php

$con = mysql_connect($host,$uname,$pwd) or die("connection failed");
mysql_select_db($db,$con) or die("db selection failed");

$name=$_POST['name'];
 $response["matier"] = array();

$r=mysql_query("SELECT class FROM classadmin WHERE cycle= '".$name."'",$con);
while($row=mysql_fetch_array($r))
{


   $product["class"] = $row["class"];
       array_push($response["matier"], $product);

//echo $fin."<br>";
}
print(json_encode($response));
mysql_close($con);
?>

and this is the result of json

{"matier":[{"class":"azerty"}]}

Upvotes: 1

Views: 133

Answers (1)

Blackbelt
Blackbelt

Reputation: 157457

the problem lies here

  while ((line = reader.readLine()) != null) {
     //     sb.append(line + "\n");
  }

commenting the sb.append, you are not filling up the StringBuilder with the data you get from the network, leaving it empty. An empty string is not a valid JSON, and this is causing your exception. Since you are still using the Apache HttpClient, the easiest way to convert the Entity to String is to use,

 EntityUtils.toString(entity)

Edit:

try {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://xxxxx/getclassprof.php");
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    result = EntityUtils.toString(entity);
    Log.e("pass 1", "connection success ");
} catch (Exception e) {
    Log.e("Fail 1", e.toString());
    Toast.makeText(getActivity(), "Invalid IP Address",
            Toast.LENGTH_LONG).show();
}

// here goes the code to parse result

Upvotes: 1

Related Questions