服务器响应400:使用android进行发布请求时必填字段

时间:2018-09-10 11:03:49

标签: android python django apache jinja2

如果我仅单击post方法,则服务器端Django响应,但是当我在输入数据后从移动字段进行sedn时,由于没有输入任何内容,Android调试出现了400错误的相同响应

enter image description here

使用调试器时服务器的响应状态为400,我正在使用getErrorStream()方法来获取特定的问题,但是我仍在输入数据,但是服务器的响应是我需要该特定字段以输入数据。标题字段在数据库中是必需的,而其他字段不是必需的。检查...

enter image description here

服务器代码:

私有类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方法中单击按钮的代码

    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();
            }
        }
    });

0 个答案:

没有答案