Android Empty data fom Json object into database through PHP

I am trying to get all the Json data from my Mysql database through php, I need to get the student name though json object using php..but I faced a problems is failed to get the data {“error”:true,”message”:”Category name is missing!”} which is caused by

if (isset($_GET["subject_code"])  ) {
$subject_code= $_GET['subject_code'];

However, if i replace SELECT * FROM student WHERE subject_code = ‘BITS 3323 Local Area Network’ it will success get stu_name.

My php code is given below:

<?php
include_once './DbConnect.php';

 function getCategories(){

// array for json response


if (isset($_GET["subject_code"])  ) {
$subject_code= $_GET['subject_code'];


$db = new DbConnect();
  $response = array();
$response["categories"] = array();

// Mysql select query
$result = mysql_query("SELECT * FROM student WHERE subject_code = '$subject_code' ");
  //$result = mysql_query("SELECT * FROM student WHERE subject_code = '$subject_code' AND section = '$section' AND session = '2-2014/2015'");


while($row = mysql_fetch_array($result)){
    // temporary array to create single category
    $tmp = array();

    $tmp["stu_name"] = $row["stu_name"];
     $tmp["stu_ID"] = $row["stu_ID"];

    // push category to final json array
    array_push($response["categories"], $tmp);
}

// keeping response header to json
header('Content-Type: application/json');

// echoing json result
echo json_encode($response);
  }

else {
    $response["error"] = true;
    $response["message"] = "Category name is missing!";
}

// echo json response
echo json_encode($response);

}
getCategories();
?>

My java AsyncTaskAsyncTask code

  /**
 * Async task to get all food categories
 * */
private class GetCategories extends AsyncTask<String, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(EditSelfInfo3.this);

        pDialog.setCancelable(false);
        pDialog.show();

    }



    @Override
    protected Void doInBackground(String... arg0) {


        List<NameValuePair> params1 = new ArrayList<NameValuePair>();
        params1.add(new BasicNameValuePair("subject_code ", subject_code ));


        ServiceHandler jsonParser = new ServiceHandler();
        String json = jsonParser.makeServiceCall(URL_CATEGORIES, ServiceHandler.GET,params1);

        Log.e("Response: ", "> " + json);

        if (json != null) {
            try {
                JSONObject jsonObj = new JSONObject(json);
                if (jsonObj != null) {
                    JSONArray categories = jsonObj
                            .getJSONArray("categories");                        

                    for (int i = 0; i < categories.length(); i++) {
                        JSONObject catObj = (JSONObject) categories.get(i);
                        Category cat = new Category(catObj.getString("stu_ID"),
                                catObj.getString("stu_name"));
                        categoriesList.add(cat);
                    }
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

        } else {
            Log.e("JSON Data", "Didn't receive any data from server!");
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        if (pDialog.isShowing())
            pDialog.dismiss();
        populateSpinner();
    }

}

The subject_code data which is getting from previous activity and to EditSelfInfo3.java through intent .

EditSelfInfo3 .java

    // getting student details from intent
    Intent i = getIntent();
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    class_ID = i.getStringExtra("class_ID");
    subject_code = i.getStringExtra("subject_code");
    section = i.getStringExtra("section");
    session = i.getStringExtra("session");

Previous activity java

    Intent in = new Intent(getApplicationContext(),
                    EditSelfInfo3.class);
            // sending pid to next activity
            in.putExtra("class_ID", class_ID);
            in.putExtra("subject_code", subject_code);
            in.putExtra("section", section);
            in.putExtra("session", session);

            // starting new activity and expecting some response back
            startActivityForResult(in, 100);


Source: json

Leave a Reply