Android json php mysql只返回单行

时间:2018-04-21 07:33:37

标签: php android mysql json

我正在开发一个应用程序,用于从mysql数据库中检索数据并以表格格式在Android应用程序中显示它。但我的应用程序只检索表格的第一行。

  

这是我的php代码

<?php

include 'db_connection.php';

$conn = OpenCon();

if(mysqli_connect_error($conn))

{

echo "Failed to Connect to Database ".mysqli_connect_error();

}



$sql="SELECT * FROM asadharap";

$result=mysqli_query($conn,$sql);


    while($row=mysqli_fetch_array($result))

    {
        $flag[]=$row;   
    }   

echo json_encode($flag);

CloseCon($conn);

?>
  

这是我的下载代码

public class Downloader扩展了AsyncTask {

Context c;
TableView tb;
String urlAddress="My link";
ProgressDialog pd;

public Downloader(Context c, TableView tb) {
    this.c = c;
    this.tb = tb;
}

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

@Override
protected String doInBackground(Void... params)
{
    try {
        URL url = new URL(urlAddress);
        HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
        httpURLConnection.setRequestMethod("GET");
        httpURLConnection.setDoInput(true);
        httpURLConnection.setDoOutput(false);
        InputStream inputStream = httpURLConnection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
        StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = bufferedReader.readLine()) != null) {
            result.append(line);
        }
        bufferedReader.close();
        inputStream.close();
        httpURLConnection.disconnect();
        return result.toString();
    }  catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

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

    pd.dismiss();

    if(s==null)
    {
        Toast.makeText(c,"Unsuccessful,Null returned",Toast.LENGTH_SHORT).show();
    }else {
        //CALL DATA PARSER TO PARSE
        DataParser parser=new DataParser(c,tb,s);
        parser.execute();
    }
}

}

  

这是我的数据解析代码

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

Context context;
private SimpleTableDataAdapter adapter;
ArrayList<Spacecraft> spacecrafts = new ArrayList<>();
TableView tb;
String jsonData;

ProgressDialog pd;

public DataParser(Context c, TableView tb, String jsonData) {
    this.context = c;
    this.tb = tb;
    this.jsonData = jsonData;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();

    pd=new ProgressDialog(context);
    pd.setTitle("Parse");
    pd.setMessage("Parsing..Please Wait");
    pd.show();
}

@Override
protected Integer doInBackground(Void... params)
{
    try {
        JSONArray ja = new JSONArray(jsonData);
        JSONObject jo = null;
        Spacecraft sp;
        for (int i = 0; i < ja.length(); i++) {
            jo = ja.getJSONObject(i);
            String sdate = jo.getString("date_of_workout");
            Date date = new SimpleDateFormat("yy-mm-dd").parse(sdate);
            String name = jo.getString("workout_name");
            String type = jo.getString("workout_type");
            int sets = jo.getInt("no_of_sets");
            float weight = (float) jo.getDouble("weight");

            sp = new Spacecraft();
            sp.setDate(date);
            sp.setName(name);
            sp.setType(type);
            sp.setSets(sets);
            sp.setWeight(weight);

            spacecrafts.add(sp);

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

    return 0;
}

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

    pd.dismiss();

    if(result==0)
    {
        Toast.makeText(context,"Unable to parse",Toast.LENGTH_SHORT).show();
    }else
    {
        adapter = new SimpleTableDataAdapter(context, new TableHelper(context).returnSpaceProbesArray(spacecrafts));
        tb.setDataAdapter(adapter);

        tb.addDataClickListener(new TableDataClickListener() {
            @Override
            public void onDataClicked(int rowIndex, Object clickedData) {
                Toast.makeText(context, ((String[])clickedData)[1], Toast.LENGTH_SHORT).show();

            }
        });

    }
}

}

  

我在下载程序的postexecute

中调用了dataparsing构造函数
protected void onPostExecute(String s) {
    super.onPostExecute(s);

    pd.dismiss();

    if(s==null)
    {
        Toast.makeText(c,"Unsuccessful,Null returned",Toast.LENGTH_SHORT).show();
    }else {
        //CALL DATA PARSER TO PARSE
        DataParser parser=new DataParser(c,tb,s);
        parser.execute();
    }
}

1 个答案:

答案 0 :(得分:0)

你在 DataParser doInBackground()中犯了一个错误。 你在for循环中返回了1。 请在完成for循环后添加返回1

以下是更正。

@Override
protected Integer doInBackground(Void... params)
{
    try {
        JSONArray ja = new JSONArray(jsonData);
        JSONObject jo = null;
        Spacecraft sp;
        for (int i = 0; i < ja.length(); i++) {
            jo = ja.getJSONObject(i);
            String sdate = jo.getString("date_of_workout");
            Date date = new SimpleDateFormat("yy-mm-dd").parse(sdate);
            String name = jo.getString("workout_name");
            String type = jo.getString("workout_type");
            int sets = jo.getInt("no_of_sets");
            float weight = (float) jo.getDouble("weight");

            sp = new Spacecraft();
            sp.setDate(date);
            sp.setName(name);
            sp.setType(type);
            sp.setSets(sets);
            sp.setWeight(weight);

            spacecrafts.add(sp);


        }

        return 1;

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

    return 0;
}