[英]android studio post request volley not returning search
我正在數據庫中搜索音樂,我在快速ios中完成了同樣的操作,沒有任何麻煩。 但是,當我在Java中嘗試相同的操作時,我只是獲取完整的表格,而不僅僅是我要搜索的曲目。 我是android studio和java的新手。 我正在使用凌空抽煙,但我找不到問題。 似乎事情沒有被正確發布或被php更正。 真正令我感到困惑的是,當我嘗試在瀏覽器中查詢數據庫時,我也會遇到錯誤。 當我嘗試從ios應用程序執行此操作時,我得到了正確的輸出,並且它已正確搜索了數據庫。
這是我的Android代碼
private void searchsong() {
String submitUrl = SEARCH_SONG_URL+"?searchWord="+searchWord;
Log.d("search song Json url", submitUrl);
JsonArrayRequest req2 = new JsonArrayRequest(Request.Method.POST, submitUrl,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
// update the data in your custom method.
mJSONAdapter.updateData(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
}
}){
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
Log.d("Submitting searchword", searchWord);
params.put("searchWord", searchWord);
Log.v("Params", "" + params);
return params;
}
};
// Adding request to request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(req2);
}
以下是我正在使用的php代碼。 這很容易。 當我包裹在S_post標簽中時它也不起作用
<?php
header('Content-type: application/json');
//if($_POST) {
$searchWord = $_POST['searchWord'];
//open connection to mysql db
$connection = mysqli_connect("$host","$db_user","$db_password","$db_name") or die("Error " . mysqli_error($connection));
//fetch table rows from mysql db
$sql = 'SELECT * FROM tracks WHERE track_name LIKE "' . $searchWord . '%"' . 'OR artist LIKE "% ' . $searchWord . '%"'. 'OR tags LIKE "% ' . $searchWord . '%"';
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
//create an array
$emparray = array();
while($row =mysqli_fetch_assoc($result))
{
$emparray[] = $row;
}
echo json_encode($emparray);
//close the db connection
mysqli_close($connection);
//}/*end if POST*/
?>
似乎我的android代碼沒有發布到php文件,但是當我查看logcat時,它顯示了一個URL,上面寫着php?searchWord = .....我完全不知道自己在做什么。 我已經建立了一個檢查用戶憑據的登錄腳本,並且工作正常。 我為為什么這不起作用感到困惑。
謝謝您幫忙
您不會像預期的那樣發送post params。 2個解決方案:
1)將請求更改為GET
這里
JsonArrayRequest req2 =新的JsonArrayRequest(Request.Method.POST,submitUrl,
和這里
$ searchWord = $ _POST ['searchWord'];
您可以刪除以下內容:
> @Override
> protected Map<String, String> getParams() {
> Map<String, String> params = new HashMap<String, String>();
>
> Log.d("Submitting searchword", searchWord);
>
> params.put("searchWord", searchWord);
>
> Log.v("Params", "" + params);
>
> return params;
>
>
> }
選項2:使用POST,但作為application / x-www-form-urlencoded發送
在您的請求中覆蓋此內容:
public String getBodyContentType() {
return "application/x-www-form-urlencoded; charset=" + getParamsEncoding();
}
/**
* Returns the raw POST or PUT body to be sent.
*
* <p>By default, the body consists of the request parameters in
* application/x-www-form-urlencoded format. When overriding this method, consider overriding
* {@link #getBodyContentType()} as well to match the new body format.
*
* @throws AuthFailureError in the event of auth failure
*/
public byte[] getBody() throws AuthFailureError {
Map<String, String> params = getParams();
if (params != null && params.size() > 0) {
return encodeParameters(params, getParamsEncoding());
}
return null;
}
/**
* Converts <code>params</code> into an application/x-www-form-urlencoded encoded string.
*/
private byte[] encodeParameters(Map<String, String> params, String paramsEncoding) {
StringBuilder encodedParams = new StringBuilder();
try {
for (Map.Entry<String, String> entry : params.entrySet()) {
encodedParams.append(URLEncoder.encode(entry.getKey(), paramsEncoding));
encodedParams.append('=');
encodedParams.append(URLEncoder.encode(entry.getValue(), paramsEncoding));
encodedParams.append('&');
}
return encodedParams.toString().getBytes(paramsEncoding);
} catch (UnsupportedEncodingException uee) {
throw new RuntimeException("Encoding not supported: " + paramsEncoding, uee);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.