【问题标题】:Android not able to receive multi records via APIAndroid 无法通过 API 接收多条记录
【发布时间】:2021-07-12 19:59:48
【问题描述】:

我正在构建一个基本的 android 应用程序来插入和读取应用程序中的记录。我能够从 MYSQL 数据库中读取单条记录并将其显示在活动页面上,但是当查询返回多条记录时,我无法显示它们。我想以表格形式显示它们

我正在调用 API,下面是我的代码:

$stmt = $conn->prepare("SELECT col1, col2 FROM my_table WHERE col3 = ?"); 
$stmt->bind_param("s",$id);
$stmt->execute();
$stmt->bind_result($col1, $col2);
$stmt->fetch();
                
$user = array(
                'col1'=>$col1, 
                'col2'=>$col2
                );
                            
$stmt->close();
$response['error'] = false; 
$response['message'] = 'Records Pulled Successfully.'
$response['user'] = $user; 

下面是我的 mainactivity.java 代码:

package com.javapaper.test;

public class MainActivity extends AppCompatActivity {

    

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

        

        //if the user is not logged in
        //starting the login activity
        if (!SharedPrefManager.getInstance(this).isLoggedIn()) {
            finish();
            startActivity(new Intent(this, LoginActivity.class));
        }
       
        User user = SharedPrefManager.getInstance(this).getUser();
        textViewUsername.setText(user.getUsername());
        col3 = user.getId();

        getdatafromdb(col3);  

        //when the user presses logout button
        //calling the logout method
       findViewById(R.id.btn_LogoutPlaceOrd).setOnClickListener(new View.OnClickListener() {
          @Override
         public void onClick(View view) {
            finish();
           SharedPrefManager.getInstance(getApplicationContext()).logout();
        }
       });
    }

    private void getdatafromdb(int col3) {
        
        //if it passes all the validations

        class getdatafromdb extends AsyncTask<Void, Void, String> {

            private ProgressBar progressBar;

            @Override
            protected String doInBackground(Void... voids) {
                //creating request handler object
                RequestHandler requestHandler = new RequestHandler();

                //creating request parameters
                HashMap<String, String> params = new HashMap<>();
                params.put("col3", col3);
                

                //returing the response
                return requestHandler.sendPostRequest(URLs.URL_PLACEORD, params);
            }

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                //displaying the progress bar 
                progressBar = (ProgressBar) findViewById(R.id.pb_placeord);
                progressBar.setVisibility(View.VISIBLE);
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                //hiding the progressbar after completion
                progressBar.setVisibility(View.GONE);

                try {
                    //converting response to json object
                    JSONObject obj = new JSONObject(s);

                    //if no error in response
                    if (!obj.getBoolean("error")) {
                        Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();

                        //getting the order id from the response

                        JSONObject userJson = obj.getJSONObject("col1");

                        //creating a new user object 
                        User user = new User(
                                userJson.getInt("col1"),
                                userJson.getString("col2"),
                                
                        );


// need help here if above is the right way to receive multiple rows and then how to display them on listview or tableview]]

                                              
                    } else {
                        Toast.makeText(getApplicationContext(), "Some error occurred", Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }

        //executing the async task
        Placeorder ru = new Placeorder();
        ru.execute();
    }

}

【问题讨论】:

    标签: android mysql android-listview


    【解决方案1】:
    $stmt = $conn->prepare("SELECT col1,col2 FROM my_table where col3 = ?"); 
                                $stmt->bind_param("s",$var);
                                    $stmt->execute();
                                                            
                                    $stmt->bind_result($col1, $col2);
                                
                                   $stmt->store_result();
                                                             
                          $n = '0';
                            while($obj =$stmt->fetch()) 
                                {
                                           
                                    $user[$n] = array(
                                    'ret_var1'=>$col1, 
                                    'ret_var2'=>$col2
                                    
                                    
                                );
    
                                                          $n = $n+1;
                                                    }//while
                                                 $stmt->close();
                            
                                           
                      $response['user'] = $user;
                      $response['error'] = false; 
                  $response['message'] = 'Data Pulled Successfully. Total Records are-' .$n;
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多