Android 403当我使用HttpPost向Django发送数据时禁止发送消息
我正在尝试将数据从android发送到Django应用程序。我想将数据存储在sqlite数据库中名为“mytable”的表中。以下是android代码:Android 403当我使用HttpPost向Django发送数据时禁止发送消息,android,django,sqlite,http-post,http-status-code-403,Android,Django,Sqlite,Http Post,Http Status Code 403,我正在尝试将数据从android发送到Django应用程序。我想将数据存储在sqlite数据库中名为“mytable”的表中。以下是android代码: try { ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); HttpClient httpclient = new DefaultHttpClient(); Http
try {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost:8000/androidweb/edit/");
JSONObject j = new JSONObject();
try {
j.put("name", "david");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
nameValuePairs.add(new BasicNameValuePair("year", j.toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// myTextView.setText(j.toString());
HttpResponse response = httpclient.execute(httppost);
myTextView.setText(response.getStatusLine().toString());
// myTextView.setText(response.toString());
}catch(Exception e) {
myTextView.setText("Error in http connection "+e.toString());
}
试试看{
ArrayList nameValuePairs=新的ArrayList();
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://localhost:8000/androidweb/edit/");
JSONObject j=新的JSONObject();
试一试{
j、 放入(“姓名”、“大卫”);
}捕获(JSONException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
添加(新的BasicNameValuePair(“年”,j.toString());
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//myTextView.setText(j.toString());
HttpResponse response=httpclient.execute(httppost);
myTextView.setText(response.getStatusLine().toString());
//myTextView.setText(response.toString());
}捕获(例外e){
myTextView.setText(“http连接中的错误”+e.toString());
}
现在问题解决了。我只需要有一个返回值听起来像Django的跨站点请求伪造框架,默认情况下它可以阻止第三方POST请求。请阅读Django的详细信息。您的任何代码都不应生成403。Django圆木是什么样子的?嘿!如何检查Django日志?错误可能是因为我没有从views.py返回任何内容吗?如果您正在运行开发服务器,请查看屏幕。什么都不退会造成500英镑的损失。嘿!我照你说的做了。Django的屏幕返回“POST/androidweb/edit/HTTP/1.1”4032326。将项目置于调试模式并再次运行。嘿!我认为你是对的。我已经降级到Django 1.1.1,不必处理csrf安全措施。然而,现在我得到500-内部服务器错误。知道为什么会发生这种情况吗?500个错误通常是由于代码中的错误造成的。如果您在Django 1.2下没有看到它们,那么我猜您的代码中可能有一些仅适用于Django 1.2以后版本的代码-可能您正在导入模块或调用1.1中不存在的方法?你的HTTP错误日志应该会给你一些线索。是的,你是对的。我的代码一定有问题。我会弄明白的