[英]Android Volley onResponse method not executing (GET request)
[英]Android: Volley String request onresponse is not called
我正在嘗試使用POST方法將數據從android發送到php
php部分單獨測試,它100%工作,它返回一個假想的json字符串
android部分不工作它不會打印錯誤甚至是所需的響應
這是代碼並且經過測試,使用Toast打印字符串“test”,但它在凌空代碼中沒有變化:(
這是php代碼:
<?php
require_once(__DIR__.'/../dbConnect.php');
$userName=$_POST['username'];
$query = "SELECT * FROM users WHERE username='$userName'";
$result = pg_query($con, $query);
$rows = pg_num_rows($result);
while ($row = pg_fetch_row($result))
{
$field = pg_num_fields( $result );
for ( $i = 0; $i < $field; $i++ ) {
$name = pg_field_name( $result, $i );
$user_arr[$name] = $row[$i];
}
}
echo json_encode($user_arr);
pg_close($con);
?>
這是android代碼:
初始化測試字符串以顯示輸出
String test = "none";
這是我稱之為方法的地方
setUserData(username);
Toast.makeText(LoginActivity.this, test, Toast.LENGTH_SHORT).show();
這是方法:
private void setUserData(final String username)
{
StringRequest strReq = new StringRequest(Request.Method.POST,
Server.GET_USERDATA_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
test= "test = "+response;
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(LoginActivity.this, volleyError.getMessage().toString(), Toast.LENGTH_LONG).show();
}
})
{
@Override
protected Map<String, String> getParams() {
// Posting parameters to getData url
Map<String, String> params = new HashMap<String, String>();
params.put(Server.KEY_USERNAME, username);
return params;
}
};
//Creating a Request Queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue.add(strReq);
}
檢查您是否已獲得互聯網許可。
還要添加吐司
onResponse()
Toast.makeText(LoginActivity.this, response.toString(), Toast.LENGTH_LONG).show();
你有一個並發問題。 當您執行setUserData(username)
方法時,Volley正在后台線程上完成工作。 因此,您需要等待響應才能在Toast中顯示它。 在你打電話的那一刻:
Toast.makeText(LoginActivity.this, test, Toast.LENGTH_SHORT).show();
服務器仍然沒有響應, test
仍然none
。 你需要從onResponse
回調中調用toast:
private void setUserData(final String username) {
StringRequest strReq = new StringRequest(
Request.Method.POST,
Server.GET_USERDATA_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
test = "test = "+response;
showToast();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(LoginActivity.this, volleyError.getMessage().toString(), Toast.LENGTH_LONG).show();
}
})
{
@Override
protected Map<String, String> getParams() {
// Posting parameters to getData url
Map<String, String> params = new HashMap<String, String>();
params.put(Server.KEY_USERNAME, username);
return params;
}
};
//Creating a Request Queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue.add(strReq);
}
private void showToast() {
Toast.makeText(LoginActivity.this, test, Toast.LENGTH_SHORT).show();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.