首页 > 解决方案 > 服务器响应 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();
            }
        }
    });

标签: androidpythondjangoapachejinja2

解决方案


推荐阅读