Android 无法通过 API 接收多条记录
Posted
技术标签:
【中文标题】Android 无法通过 API 接收多条记录【英文标题】:Android not able to receive multi records via 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();
【问题讨论】:
【参考方案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;
【讨论】:
以上是关于Android 无法通过 API 接收多条记录的主要内容,如果未能解决你的问题,请参考以下文章
使用 mongoose 将从 JSON 接收的多条记录插入到集合中
无法通过 jconn4.jar/Sybase 驱动版本 7 & Mybatis 框架更新 Sybase 数据库中的多条记录