简体   繁体   English

Android HttpURLConnection getResponseCode()400-为什么?

[英]Android HttpURLConnection getResponseCode() 400 - why?

I had used below code for sending data to server in several projects and it works , but in my recent project it doesnt work and give me statuscode 400 ... i use this code in AsyncTask class : 我曾在以下项目中使用以下代码向服务器发送数据,它可以工作,但在我最近的项目中却无法工作,并给我statuscode 400 ...我在AsyncTask类中使用以下代码:

public class sendingDataAsyncHttpTask extends AsyncTask<String, Void, Integer> {

    @Override
    protected void onPreExecute() {

    }

    @Override
    protected Integer doInBackground(String... params) {

        InputStream inputStream = null;
        Integer result = 0;
        HttpURLConnection urlConnection = null;
        String key = params[0];
        try {
            URL url = new URL(params[0]);
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            int statusCode = urlConnection.getResponseCode();
            if (statusCode == 200) {
                BufferedReader r = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                StringBuilder response = new StringBuilder();
                String line;
                while ((line = r.readLine()) != null) {
                    response.append(line);
                }
                parseAdminTaskResult(response.toString());
                result = 1;
            } else {
                result = 0;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    @Override
    protected void onPostExecute(Integer result) {
}

上面的代码可以正常工作,但是问题出在我尝试发送的数据中……请始终确保您的数据中没有空格(“”),然后将其发送到服务器...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM