Ответ Django на стороне сервера, если я щелкаю только по методу post, но когда после ввода данных я отключаюсь от мобильных полей, такой же ответ приходит на отладку Android с ошибкой 400, поскольку ничего не было введено
В ответе сервера при использовании отладчика указывается ответ 400, и я использую метод getErrorStream()
, чтобы получить конкретную проблему, но, тем не менее, я ввожу данные, но ответ от сервера заключается в том, что мне нужен этот конкретный поле для ввода данных. Поле заголовка является обязательным в базе данных, но другие поля не являются. Проверьте ...
Код сервера:
закрытый класс SendDeviceDetails extends 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
}
}
Код нажатия кнопки, который находится в методе создания
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();
}
}
});