解析数据或执行数据错误

时间:2017-06-14 15:32:12

标签: java android

我有这个应用程序应用程序从数据库中提取数据并将其存储在列表视图中,然后每个记录都有我想要的历史记录,当我点击ListView中的记录来向我显示该记录的历史记录。现在,当我点击一条记录然后点击查找ID按钮它会给我记录的ID然后我将点击历史记录以清除此ListView并在同一ListView上显示该记录的历史记录时。  Screen Shot for the app1Screen Shot for the app2

任何人都可以帮助我,我的应用程序运行,但当我想显示历史记录它没有通过这个(如果)我不知道为什么

@Override
    protected void onPostExecute(Integer integer) {
        super.onPostExecute(integer);

        if(integer == 1){

            ArrayAdapter<String > adapter= new ArrayAdapter<String>(c,android.R.layout.simple_list_item_1, patients );
            lv.setAdapter(null);
            lv.setAdapter(adapter);
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                    Snackbar.make(view,patients.get(i), Snackbar.LENGTH_SHORT).show();
                }
            });

        }else{
            Toast.makeText(c,"Unable to parse data", Toast.LENGTH_SHORT).show();
        }
        progressDialog.dismiss();
    }

PHP方

<?php
$host='127.0.0.1';
$username='root';
$password='';
$database='app';

$con =mysqli_connect($host, $username, $password, $database) or die ('unable to connect');

if (mysqli_connect_error($con))
{
echo "Failed to connect to Database ".mysqli_connect_error();

}
$patientID = $_GET['patientID'];
$query= mysqli_query($con, "SELECT * FROM history where 
   patientID='$patientID' ");

if ($query)
{

    while ($row = mysqli_fetch_array($query))
    {
        $flag[]= $row;
    }
    print(json_encode($flag));
}

mysqli_close($con);



    ?>

HistoryDownloader Java类

public class HistoryDownloader extends AsyncTask<Void, Integer, String> {





    Context c;
    String address;
    ListView lv;

    ProgressDialog progressDialog;


    public HistoryDownloader(Context c, String address, ListView lv) {
        this.c = c;
        this.address = address;
        this.lv = lv;

    }

    //Before the job start

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog= new ProgressDialog(c);
        progressDialog.setTitle("Fetch Data");
        progressDialog.setMessage("Fetching data .... Please wait ");
        progressDialog.show();

    }

    @Override
    protected String doInBackground(Void... strings) {
        String data= downloadData();

        return data;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

        progressDialog.dismiss();


        if (s != null){

            HistoryParser h= new HistoryParser(c,lv,s);
            h.execute();

        }else{
            Toast.makeText(c, "Unable to download data ", Toast.LENGTH_SHORT).show();
        }
    }

    private  String downloadData(){
        //connect and get a stream

        InputStream is= null;
        String line =null;

        try{
            URL url = new URL(address);
            HttpURLConnection con= (HttpURLConnection) url.openConnection();

            is =new BufferedInputStream( con.getInputStream());

            BufferedReader br= new BufferedReader(new InputStreamReader(is));

            StringBuffer sb= new StringBuffer() ;

            if(br !=null){
                while((line=br.readLine()) !=null){
                    sb.append(line+"\n");

                }

            }
            else{
                return null;

            }
            return  sb.toString();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return  null;
    }
}

HistoryParser java Class

public class HistoryParser  extends AsyncTask<Void, Integer, Integer> {

    String lls;
    Context c;
    ListView lv;
    String data;

    ArrayList<String > patients= new ArrayList<>();

    ProgressDialog progressDialog;

    public HistoryParser (Context c, ListView lv, String data) {
        this.c = c;
        this.lv = lv;
        this.data = data;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog= new ProgressDialog(c);
        progressDialog.setTitle("Parser");
        progressDialog.setMessage("Parsing the data ... please wait");
        progressDialog.show();


    }

    @Override
    protected Integer doInBackground(Void... voids) {

        return this.histoParse();

    }

    @Override
    protected void onPostExecute(Integer integer) {
        super.onPostExecute(integer);

        if(integer == 1){

            ArrayAdapter<String > adapter= new ArrayAdapter<String>(c,android.R.layout.simple_list_item_1, patients );
            lv.setAdapter(null);
            lv.setAdapter(adapter);
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                    Snackbar.make(view,patients.get(i), Snackbar.LENGTH_SHORT).show();
                }
            });

        }else{
            Toast.makeText(c,"Unable to parse data", Toast.LENGTH_SHORT).show();
        }
        progressDialog.dismiss();
    }

    // parse the reciv
    private int histoParse (){
        try {
            //adding the data  to json aray first
            JSONArray js= new JSONArray(data);

            //create json object to hold a singel item
            JSONObject jo= null;

            patients.clear();

            // loop the array
            for(int i=0 ;i<js.length();i++){
                jo= js.getJSONObject(i);
                //retriving the name
                //TODO: write the strring depend on the column name in the database
                // write the strring depend on the column name in the database
                String case1=jo.getString("case1");
               /* String case2=jo.getString("case2");
                String case3=jo.getString("Case3");
                String case4=jo.getString("Case4");
                String case5=jo.getString("Case5");
                String case6=jo.getString("Case6");
                String case7=jo.getString("Case7");
                String case8=jo.getString("Case8");
                String case9=jo.getString("Case9");
                String case10=jo.getString("Case10");
                String trt1=jo.getString("trt1");
                String trt2=jo.getString("trt2");
                String trt3=jo.getString("trt3");
                String trt4=jo.getString("trt4");
                String trt5=jo.getString("trt5");
                String trt6=jo.getString("trt6");
                String trt7=jo.getString("trt7");
                String trt8=jo.getString("trt8");
                String trt9=jo.getString("trt9");
                String trt10=jo.getString("trt10");




                //add it to our array list
                patients.add("Patient History");
                patients.add("");
                patients.add("Cases");
             patients.add("");
                */   patients.add(case1);
             /*   patients.add(case2);
                players.add(case3);
                players.add(case4);
                players.add(case5);
                players.add(case6);
                players.add(case7);
                players.add(case8);
                players.add(case9);
                players.add(case10);
                players.add("");
                players.add("Treatments");
                players.add("");
                players.add(trt1);
                players.add(trt2);
                players.add(trt3);
                players.add(trt4);
                players.add(trt5);
                players.add(trt6);
                players.add(trt7);
                players.add(trt8);
                players.add(trt9);
                players.add(trt10);
*/

            }
            return 1;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return 0;

    }



}

Main Class Java

public class MainActivity extends AppCompatActivity {

    Context context;
    String url="http://10.0.2.2/Android/Fetch.php";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        final TextView textView= (TextView) findViewById(R.id.textView);
        final ListView lv=(ListView) findViewById(R.id.lv);
        final Downloader d= new Downloader(this,url,lv);
        String urlHistory="http://10.0.2.2/Android/History.php?patientID="+textView.getText().toString().trim();
        final HistoryDownloader dd= new HistoryDownloader(this,urlHistory,lv);

        final Button btn= (Button ) findViewById(R.id.button);
        final Button btn2=(Button)  findViewById(R.id.button2);



        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                lv.setAdapter(null);
                //String kk=textView.getText().toString().trim();
              dd.execute();
                if(textView.getText() != ""){

                }else{

                }
            }
        });

1 个答案:

答案 0 :(得分:0)

i did not use Transmitting Network Data Volley thats why im getting :)