此代码应该将我的JSON值发送到服务器。当我点击按钮时,出现错误 - Unexpected reponse code 404.
有人可以解释问题是什么以及如何解决它?
这是服务器端错误吗?
这是我的代码......
b2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
getResults();
StringRequest request=new StringRequest(Request.Method.POST,server_url,new Response.Listener<String>() {
@Override
public void onResponse(String response){
}
},new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error) {
}
})
{
protected Map<String,String>getParams() throws AuthFailureError{
Map<String,String> parameters = new HashMap<String, String>();
getResults().put(parameters);
return parameters;
}
};
requestQueue.add(request);
}
});
}
private JSONArray getResults() {
String myPath = "/data/data/com.example.sebastian.patientdetails/databases/" + "MyDBName.db";
String myTable = "patients";
SQLiteDatabase myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
String searchQuery = "SELECT * FROM " + myTable;
Cursor cursor = myDataBase.rawQuery(searchQuery, null);
JSONArray resultSet = new JSONArray();
cursor.moveToFirst();
JSONArray jsonArray = null;
while (!cursor.isAfterLast()) {
int totalColumn = cursor.getColumnCount();
JSONObject rowObject = new JSONObject();
jsonArray = new JSONArray();
for (int i = 0; i < totalColumn; i++) {
if (cursor.getColumnName(i) != null) {
JSONObject object = new JSONObject();
try {
if (cursor.getString(i) != null) {
Log.d("TAG_NAME", cursor.getString(i));
object.put(cursor.getColumnName(i), cursor.getString(i));
} else {
object.put(cursor.getColumnName(i), "");
}
jsonArray.put(object);
}
catch (Exception e) {
Log.d("TAG_NAME", e.getMessage());
}
}
}
jsonArray.put(rowObject);
resultSet.put(rowObject);
cursor.moveToNext();
}
return resultSet;
}
}
答案 0 :(得分:1)
404表示未找到,server_url
指向不存在的网址,或者您请求的网页将您重定向到404。
如果您打开浏览器并将AJAX中的POST请求发送到相同的URL和相同的参数,那么您应该会遇到相同的行为。