android - 服务器响应 400:使用 android 发布请求时需要字段
问题描述
服务器端 Django 响应,如果我仅单击 post 方法,但是当我在输入数据后从移动字段 sedn 时,由于没有输入任何内容,所以相同的响应会出现 400 错误的 android 调试
使用调试器时服务器的响应状态为 400 响应,我正在使用该getErrorStream()
方法来获取特定问题,但我仍在输入数据,但来自服务器的响应是我需要该特定字段来输入数据。标题字段在数据库中是强制性的,但其他字段不是。查看...
服务器代码:
私有类 SendDeviceDetails 扩展 AsyncTask {
@Override
protected String doInBackground(String... params) {
String data = "";
HttpURLConnection httpURLConnection = null;
try {
httpURLConnection = (HttpURLConnection) new URL(params[0]).openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
wr.writeBytes("PostData=" + params[1]);
wr.flush();
wr.close();
int code = httpURLConnection.getResponseCode();
System.out.println("Response code of the object is "+code);
InputStream in;
if (code >= 200 && code < 400) {
// Create an InputStream in order to extract the response object
in = httpURLConnection.getInputStream();
}
else {
in = httpURLConnection.getErrorStream();
}
Log.d("FOR_LOG", String.valueOf(httpURLConnection));
InputStreamReader inputStreamReader = new InputStreamReader(in);
int inputStreamData = inputStreamReader.read();
while (inputStreamData != -1) {
char current = (char) inputStreamData;
inputStreamData = inputStreamReader.read();
data += current;
}
} catch (IOException e1) {
e1.printStackTrace();
} finally {
if (httpURLConnection != null) {
httpURLConnection.disconnect();
}
}
return data;
}private class SendDeviceDetails extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String data = "";
HttpURLConnection httpURLConnection = null;
try {
httpURLConnection = (HttpURLConnection) new URL(params[0]).openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
wr.writeBytes("PostData=" + params[1]);
wr.flush();
wr.close();
int code = httpURLConnection.getResponseCode();
System.out.println("Response code of the object is "+code);
InputStream in;
if (code >= 200 && code < 400) {
// Create an InputStream in order to extract the response object
in = httpURLConnection.getInputStream();
}
else {
in = httpURLConnection.getErrorStream();
}
Log.d("FOR_LOG", String.valueOf(httpURLConnection));
InputStreamReader inputStreamReader = new InputStreamReader(in);
int inputStreamData = inputStreamReader.read();
while (inputStreamData != -1) {
char current = (char) inputStreamData;
inputStreamData = inputStreamReader.read();
data += current;
}
} catch (IOException e1) {
e1.printStackTrace();
} finally {
if (httpURLConnection != null) {
httpURLConnection.disconnect();
}
}
return data;
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.e("TAG", result); // this is expecting a response code to be sent from your server upon receiving the POST data
}
}
Oncreate方法中的按钮Click代码
title = findViewById(R.id.sendtitle);
platform = (EditText)findViewById(R.id.sendtechnology);
date = (EditText)findViewById(R.id.senddate);
description = (EditText)findViewById(R.id.sendescription);
Button submitButton = findViewById(R.id.uploaddata);
submitButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
JSONObject postData = new JSONObject();
try {
//Button image = (Button)findViewById(R.id.sendImage);
postData.put("title", title.getText().toString());
postData.put("technology", platform.getText().toString());
postData.put("date", date.getText().toString());
postData.put("description", description.getText().toString());
//postData.put("image", image.getText().toString());
//postData.put("deviceID", deviceID.getText().toString());
if((title == null) ){
Toast.makeText(getApplicationContext(),"Please Enter Title",Toast.LENGTH_SHORT).show();
}
else {
new SendDeviceDetails().execute("http://url/data/details/", postData.toString());
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
解决方案
推荐阅读
- c++ - 有没有办法在 C++ 源代码中用空格编写大量数字以使其更具可读性?
- mysql - 带参数的 MySQL 函数出错
- .htaccess - 无法重定向查询字符串 url
- openshift - openshift 指标部署程序错误
- javascript - 使用 fs-extra 和 Chokidar node.js 同步文件
- javascript - 如何使用 ci jquery ajax 对加入表进行分组?
- python - 直接在 NLTK 模式中应用字符串
- ios - 无法为 Facebook Graph API 用户的照片边缘获取单个大尺寸
- python - logits 的形状和标签不匹配
- webview - Window.open() 在 WKWebView 中不起作用