从Php中重新获取值时出错

时间:2017-04-12 17:22:26

标签: php android mysql

我的Java文件:

公共类ReportStudent2扩展了Activity {

Activity context;

HttpPost httppost;

StringBuffer buffer;

HttpResponse response;

HttpClient httpclient;

ProgressDialog pdnew;

ListAdapter3 adapternew;

ListView listProduct;

ArrayList<Product3> records;

protected void onCreate(Bundle savedInstanceState) {

    //TODO Auto-generated method stub

    super.onCreate(savedInstanceState);

    setContentView(R.layout.report_student2);

    context=this;

    records=new ArrayList<Product3>();

    listProduct=(ListView)findViewById(R.id.product_list1);

    adapternew= new ListAdapter3(context, R.layout.model_report_student2,R.id.pro_name2,
            records);

    listProduct.setAdapter(adapternew);

}

public void onStart(){

    super.onStart();

    //execute background task

    BackTask bt=new BackTask();

    bt.execute();



}



//background process to make a request to server and list product information

private class BackTask extends AsyncTask<Void,Void,Void>{

    protected void onPreExecute(){

        super.onPreExecute();

        pdnew = new ProgressDialog(context);

        pdnew.setTitle("Retrieving data");

        pdnew.setMessage("Please wait.");

        pdnew.setCancelable(true);

        pdnew.setIndeterminate(true);

        pdnew.show();



    }



    protected Void doInBackground(Void...params){



        InputStream is=null;

        String result="";

        try{



            httpclient=new DefaultHttpClient();
            SharedPreferences sharedPreferences = getSharedPreferences(ConfigFaculty.SHARED_PREF_NAME2, Context.MODE_PRIVATE);
            String username = sharedPreferences.getString(ConfigFaculty.USERNAME_SHARED_PREF2, "Not Available");

            Intent i=getIntent();
            String course_name=i.getStringExtra("course_name");
            String url="http://192.168.43.213/Login/student_login/courses2.php?username=" +username + "&course_name=" +course_name;
            httppost= new HttpPost(new URI(url.replace(" ","%20")));

            response=httpclient.execute(httppost);

            Log.d("Ed","Request Sent");
            HttpEntity entity = response.getEntity();

            // Get our response as a String.

            is = entity.getContent();



        }catch(Exception e){



            if(pdnew!=null)

                pdnew.dismiss(); //close the dialog if error occurs

            Log.e("ERROR", e.getMessage());



        }



        //convert response to string

        try{

            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"),8);

            StringBuilder sb = new StringBuilder();

            String line = null;

            while ((line = reader.readLine()) != null) {

                sb.append(line+"\n");

            }

            is.close();

            result=sb.toString();
            Log.d("Data Parsed",result);

        }catch(Exception e){

            Log.e("ERROR", "Error converting result "+e.toString());



        }



        //parse json data

        try{

            // Remove unexpected characters that might be added to beginning of the string

            result=result.substring(result.indexOf("["));

            JSONArray jArray =new JSONArray(result);

            for(int i=0;i<jArray.length();i++){

                JSONObject json_data =jArray.getJSONObject(i);

                Product3 p=new Product3();

                p.setDate(json_data.getString("Date"));

                p.setStatus(json_data.getInt("status"));



                records.add(p);



            }





        }

        catch(Exception e){

            Log.e("ERROR", "Error parsing data "+e.toString());





        }



        return null;

    }





    protected void onPostExecute(Void result){



        if(pdnew!=null) pdnew.dismiss(); //close dialog

        Log.e("size", records.size() + "");

        adapternew.notifyDataSetChanged(); //notify the ListView to get new records



    }



}

}

dbConnect.php:

 <?php

    define('HOST','localhost');
    define('USER','root');
    define('PASS','');
    define('DB','nit_agartala');

    $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');
 ?>

courses.php

<?php

$username = $_POST['username'];
$course_name = $_POST['course_name'];
require_once('dbConnect.php');


$sql = mysqli_query($con,"SELECT Date,status FROM attendance NATURAL JOIN course WHERE stud_id = '$username' AND course_name = '$course_name'");

$result = array();
while($row1 = mysqli_fetch_array($sql)){
array_push($result,array(
    'Date'=>$row1['Date'],
    'status'=>$row1['status']
));
}

echo (json_encode($result));
mysqli_close($con);
?>

我通过参数将数据发送到php文件。但是我想通过httppost中的php body传递数据。为此我得到错误。我该怎么办?

0 个答案:

没有答案