我有问题将Android Studio变量中的值传递给php代码。在这种情况下,我想在变量" group_id"中传递值。在Message.java中的DbOperation.php。这意味着最后,函数" getMessages"在DbOperation.php里面可以得到变量" group_id"的值。并选择MySQL数据库中的特定表。我还是Android Studio新手,请帮我解决这个问题。 Tq非常。
例如:变量" group_id"的值" ABC123"," getMessages" DbOperation.php中的函数将执行" SELECT a.id,a.message,a.sentat,a.users_id,b.name FROM ABC123_messages a,users b WHERE a.users_id = b.id ORDER BY a.id ASC;"
下面的代码是Message.java的java类
@edbyellen
rugs so cozy you can walk on them, nap, on them, sleep on them you get the picture
Now available in select retailers in the US, crafted by @LoloiRugs.
这是index.php
中php代码的一部分 SharedPreferences sharedPreferences = getActivity().getSharedPreferences(Config.SHARED_PREF_GROUP, Context.MODE_PRIVATE);
group_id = sharedPreferences.getString(Config.GROUP_SHARED_PREF, "Not Available");
private void fetchMessages() {
StringRequest stringRequest = new StringRequest(Request.Method.POST, URLs.URL_FETCH_MESSAGES,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//dialog.dismiss();
try {
JSONObject res = new JSONObject(response);
JSONArray thread = res.getJSONArray("messages");
for (int i = 0; i < thread.length(); i++) {
JSONObject obj = thread.getJSONObject(i);
int userId = obj.getInt("userid");
String message = obj.getString("message");
String name = obj.getString("name");
String sentAt = obj.getString("sentat");
Message messagObject = new Message(userId, message, sentAt, name);
messages.add(messagObject);
}
adapter = new ThreadAdapter(getActivity(), messages, AppController.getInstance().getUserId());
recyclerView.setAdapter(adapter);
scrollToBottom();
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("group_id", group_id);
return params;
}
};
AppController.getInstance().addToRequestQueue(stringRequest);
}
这是DbOperation.php
中的一个功能$app->post('/messages', function () use ($app){
verifyRequiredParams(array('group_id'));
$group_id = $app->request()->post('group_id');
$db = new DbOperation();
$messages = $db->getMessages($group_id);
$response = array();
$response['error']=false;
$response['messages'] = array();
while($row = mysqli_fetch_array($messages)){
$temp = array();
$temp['id']=$row['id'];
$temp['message']=$row['message'];
$temp['userid']=$row['users_id'];
$temp['sentat']=$row['sentat'];
$temp['name']=$row['name'];
array_push($response['messages'],$temp);
}
echoResponse(200,$response);});
function verifyRequiredParams($required_fields){
$error = false;
$error_fields = "";
$request_params = $_REQUEST;
// Handling PUT request params
if ($_SERVER['REQUEST_METHOD'] == 'PUT') {
$app = \Slim\Slim::getInstance();
parse_str($app->request()->getBody(), $request_params);
}
foreach ($required_fields as $field) {
if (!isset($request_params[$field]) || strlen(trim($request_params[$field])) <= 0) {
$error = true;
$error_fields .= $field . ', ';
}
}
if ($error) {
// Required field(s) are missing or empty
// echo error json and stop the app
$response = array();
$app = \Slim\Slim::getInstance();
$response["error"] = true;
$response["message"] = 'Required field(s) ' . substr($error_fields, 0, -2) . ' is missing or empty';
echoResponse(400, $response);
$app->stop();
}}
答案 0 :(得分:0)
在android代码看起来很好。但我认为你还没有抓住php中的发送数据。使用$ _POST [&#34; group_id&#34;],检查它。