带RESTAPI的Android截击-POST不会错误地插入dB和Response
我正在使用as REST Api访问我的MySQL数据库。为了从Android应用程序访问数据,我使用了Volley库。 除POST(在db中创建新项)外,所有工作正常。但相反,我正在获取JSON的新创建项将包含所有项(看起来像GET的输出),并且该项不是在dB中创建的带RESTAPI的Android截击-POST不会错误地插入dB和Response,rest,android-volley,crud,Rest,Android Volley,Crud,我正在使用as REST Api访问我的MySQL数据库。为了从Android应用程序访问数据,我使用了Volley库。 除POST(在db中创建新项)外,所有工作正常。但相反,我正在获取JSON的新创建项将包含所有项(看起来像GET的输出),并且该项不是在dB中创建的 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "APP START");
tv = findViewById(R.id.textView);
buttonPost = findViewById(R.id.buttonPost);
buttonGet = findViewById(R.id.buttonGet);
Calendar cal = Calendar.getInstance();
SimpleDateFormat sd1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
current_date = sd1.format(new Date(cal.getTimeInMillis()));
Log.d(TAG, "current_date=" + current_date);
cache = new DiskBasedCache(getCacheDir(), 1024 * 1024); // 1MB cap
mRequestQueue = new RequestQueue(cache, network);
mRequestQueue.start();
buttonGet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "ButtonGet pressed");
tv.setText("");
getRest();
}
});
buttonPost.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "ButtonPost pressed");
tv.setText("");
postRest();
}
});
}
getRest()
tv.append(“RESTAPI-通过GET”+“\n”读取数据);
JsonObjectRequest jsObjRequest=新的JsonObjectRequest(Request.Method.GET,endpointUrl,null,new Response.Listener()){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
JSONObject vancuraLevel1=response.getJSONObject(“restdemo”);
JSONArray vancuraLevel2=vancuraLevel1.getJSONArray(“记录”);
int JSONlenght2=vancuraLevel2.length();
Log.d(“JSON”、“JSONlenght2=“+JSONlenght2”);
对于(int n=0;n
postRest(){
最终字符串whattointer=“foo subjectkt”+当前日期;
//插入后数据
append(“RESTAPI-通过POST-payload插入数据=“+whatToInsert+”\n”);
StringRequest postRequest=新的StringRequest(Request.Method.POST,endpointUrl,new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
//回应
Log.d(“响应”,响应);
//附加(当前日期+“\n”);
tv.append(“response=“+response”);
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
//错误
Log.e(“Error.Response”,Error.getMessage());
append(“ERROR”+ERROR.toString()+“\n”);
}
})
{
@凌驾
受保护的映射getParams()
{
Map params=新的HashMap();
//参数put(“索引”、“空”);
参数put(“基准”,“2017-12-30”);
参数put(“subjectkt”,whattoint);
参数put(“ovoce”、“2”);
返回参数;
}
};
//射门请求
mRequestQueue.add(postRequest);
结果得到-它是好的
故障后的结果
project在上可用。仔细查看代码,GET方法返回一个JSONObject响应,而POST方法返回一个字符串响应。POST方法的字符串响应非常正确,它的结果与GET方法的结果完全相同。您所要做的就是转换字符串响应JSON对象您将拥有与GET方法相同的JSONObject
JSONObject jsonObject = new JSONObject(response);
然后,您可以解析对象以获得通过禁用截取缓存解决的结果
getRequest.setShouldCache(false);
postRequest.setShouldCache(false);
作为一种解决方法,我使用了另一个URL for POST方法(使用另一个PHP后端),效果很好。互联网上有很多帖子说Volley与POST有问题。嘿,这里是后端的作者(mevdschee)。如果你用调试代理记录后端调用(例如Charles)我可以帮你看看哪里出了问题,但是你似乎只是在做获取请求而不是发布请求。干杯!
JSONObject jsonObject = new JSONObject(response);
getRequest.setShouldCache(false);
postRequest.setShouldCache(false);