Android - 使用参数从数据库中过滤数据

时间:2018-04-23 17:19:58

标签: java php android mysql

我正在尝试创建一个Android应用程序,其中数据被过滤并从服务器获取。为此,我想将一个字符串上传到脚本,将其插入查询并获取包含给定参数的行。在我的代码中,我设置了一个固定的字符串来测试它,但问题是它没有返回任何东西,也许你可以看到我的错。 数据库中的表只包含单个字母行

我的剧本:

<?php

include 'DatabaseConfig.php';

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {

 die("Connection failed: " . $conn->connect_error);
} 

$var = $_POST['name'];

 $query = "SELECT * FROM TestTable WHERE name = '$var'";

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

    while($row = mysqli_fetch_assoc($result)) {
            $array[] = $row;
        } 

    header('Content-Type:Application/json');
    echo json_encode($array);
    mysqli_close($conn);

?>

我的代码:

public class MainActivity extends AppCompatActivity {

    List<GetDataAdapter> GetDataAdapter1;
    RecyclerView recyclerView;
    RecyclerView.LayoutManager recyclerViewlayoutManager;
    RecyclerView.Adapter recyclerViewadapter;
    ProgressBar progressBar;

    String a = "a";
    String ServerURL = "https://abcde.com/test.php";
    EditText name;
    Button button;
    String TempName;
    String JSON_ID = "id";
    String JSON_NAME = "name";

    JsonArrayRequest jsonArrayRequest;
    com.android.volley.RequestQueue requestQueue;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        GetDataAdapter1 = new ArrayList<>();
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView1);
        progressBar = (ProgressBar) findViewById(R.id.progressBar1);
        button = (Button) findViewById(R.id.button);
        recyclerView.setHasFixedSize(true);
        recyclerViewlayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(recyclerViewlayoutManager);
        name = (EditText) findViewById(R.id.editText);


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                GetData();

                InsertData(a);

                JSON_DATA_WEB_CALL();


            }
        });
    }

    public void GetData() {

        TempName = name.getText().toString();


    }

    public void InsertData(final String a) {

        class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
            @Override
            protected String doInBackground(String... params) {

                String NameHolder = a;


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

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


                try {
                    HttpClient httpClient = new DefaultHttpClient();

                    HttpPost httpPost = new HttpPost(ServerURL);

                    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                    HttpResponse httpResponse = httpClient.execute(httpPost);

                    HttpEntity httpEntity = httpResponse.getEntity();


                } catch (ClientProtocolException e) {

                } catch (IOException e) {

                }
                return "Data Inserted Successfully";
            }

            @Override
            protected void onPostExecute(String result) {

                super.onPostExecute(result);

                Toast.makeText(MainActivity.this, "Data Submit Successfully", Toast.LENGTH_LONG).show();

            }
        }

        SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();

        sendPostReqAsyncTask.execute(a);
    }


    public void JSON_DATA_WEB_CALL() {

        jsonArrayRequest = new JsonArrayRequest(ServerURL,

                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {

                        progressBar.setVisibility(View.GONE);

                        JSON_PARSE_DATA_AFTER_WEBCALL(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });

        requestQueue = Volley.newRequestQueue(this);

        requestQueue.add(jsonArrayRequest);
    }

    public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array) {

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

            GetDataAdapter GetDataAdapter2 = new GetDataAdapter();

            JSONObject json = null;
            try {
                json = array.getJSONObject(i);

                GetDataAdapter2.setId(json.getInt(JSON_ID));

                GetDataAdapter2.setName(json.getString(JSON_NAME));


            } catch (JSONException e) {

                e.printStackTrace();
            }
            GetDataAdapter1.add(GetDataAdapter2);
        }

        recyclerViewadapter = new RecyclerViewAdapter(GetDataAdapter1, this);

        recyclerView.setAdapter(recyclerViewadapter);
    }


}

1 个答案:

答案 0 :(得分:0)

当你从数据库接收数据时,你做错了,比如

while ($row = mysqli_fetch_assoc($result)) {
 // Now do like, name of the field in your database put inside  the $row['here'];
     echo $row['username'];
     echo $row['password'];
}

现在应该工作; 感谢的