Java android studio post请求截击未返回搜索
我正在搜索我的音乐数据库,我在swift ios中也做了同样的事情,没有任何问题。然而,当我在java中尝试同样的方法时,我只是得到了完整的表,而不仅仅是我正在搜索的轨迹。我对android studio和java还很陌生。我在截击,我找不到问题所在。似乎事情没有得到正确的张贴或收到更正的php。真正让我困惑的是,当我试图在浏览器中查询数据库时,我也得到了错误的输出。当我尝试从ios应用程序执行此操作时,我得到了正确的输出,并且它已正确搜索了数据库 这是我的安卓代码Java android studio post请求截击未返回搜索,java,php,json,post,android-studio,Java,Php,Json,Post,Android Studio,我正在搜索我的音乐数据库,我在swift ios中也做了同样的事情,没有任何问题。然而,当我在java中尝试同样的方法时,我只是得到了完整的表,而不仅仅是我正在搜索的轨迹。我对android studio和java还很陌生。我在截击,我找不到问题所在。似乎事情没有得到正确的张贴或收到更正的php。真正让我困惑的是,当我试图在浏览器中查询数据库时,我也得到了错误的输出。当我尝试从ios应用程序执行此操作时,我得到了正确的输出,并且它已正确搜索了数据库 这是我的安卓代码 private void s
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);
}
private void searchsong(){
String submitur=SEARCH\u SONG\u URL+“?searchWord=“+searchWord;
Log.d(“搜索歌曲Json url”,submitur);
JsonArrayRequest REK2=新的JsonArrayRequest(Request.Method.POST,submitur,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONArray响应){
Log.d(TAG,response.toString());
//更新自定义方法中的数据。
mJSONAdapter.updateData(响应);
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
d(标记“Error:+Error.getMessage());
Toast.makeText(getApplicationContext(),
错误.getMessage(),Toast.LENGTH_SHORT).show();
}
}){
@凌驾
受保护的映射getParams(){
Map params=新的HashMap();
Log.d(“提交搜索词”,搜索词);
参数put(“搜索词”,搜索词);
Log.v(“参数”,“参数+参数”);
返回参数;
}
};
//将请求添加到请求队列
RequestQueue RequestQueue=Volley.newRequestQueue(this);
requestQueue.add(请求2);
}
下面是我正在使用的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*/
?>
您没有按预期发送post参数。2种解决方案:
1) 更改请求以获取
这里
JsonArrayRequest REK2=新的JsonArrayRequest(Request.Method.POST,
亚图尔
这里呢
$searchWord=$\u 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;
>
>
> }
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);
}
}