SDK 23及以下设备中的AsyncTask返回Null

时间:2019-02-21 10:34:57

标签: android android-asynctask sdk

我正在使用Asynctask传递API参数。 Asynctask正在执行,但Asynctask PostExecute中的字符串响应为带有SDK 23及以下版本的设备提供了空值。但是,当设备等于或高于SDK24(Nougat)时,它可以完美工作,并且将数据发送到API,但是当SDK为23并且较低的数据没有发送到API时。有人遇到这个问题吗?请启发我在代码中遗漏的内容,或者我做错了代码。非常感谢。

 private class sendToServerOfficial extends AsyncTask<String,Void,String> {

    int statusCodeone;

    String convert_txt_et_username = et_username.getText().toString();
    String convert_txt_content = et_content.getText().toString();

    @Override
     protected String doInBackground(String... strings) {
        try {
            urlURL = new URL("http://www.testingsite.com/api/sendServer?/ip="+getIPAddress+"&phone_num="+getMobilePhoneNumber+"&user_text="+convert_txt_et_username+"&content_text="+convert_txt_content);
            HttpURLConnection httpURLConnection = (HttpURLConnection) urlURL.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setRequestProperty("Content-Type","UTF-8");
            httpURLConnection.connect();
            statusCodeone = httpURLConnection.getResponseCode();
            if (statusCodeone == 200) {
                InputStream it = new BufferedInputStream(httpURLConnection.getInputStream());
                InputStreamReader read = new InputStreamReader(it);
                BufferedReader buff = new BufferedReader(read);
                StringBuilder dta = new StringBuilder();
                String chunks;

                while ((chunks = buff.readLine()) != null) {
                    dta.append(chunks);
                }

                buff.close();
                read.close();
                return dta.toString();
            }

        }
        catch (ProtocolException e) { e.printStackTrace(); }
        catch (MalformedURLException e) { e.printStackTrace(); }
        catch (IOException e) { e.printStackTrace(); }

        return null;
    }
    @Override
    protected void onPostExecute(String response) {
        Toast.makeText(MainActivity.this, response + "Form is submitted already" + urlURL, Toast.LENGTH_LONG).show();

        txt_inputURL.setEnabled(true);
        btnClick.setClickable(true);

        txt_inputURL.getText().clear();
    }
}

0 个答案:

没有答案