首页 > 解决方案 > asynctask 在带有 api 24 的模拟器中工作,而不是在带有 api 28 的真实设备中工作

问题描述

我正在使用 asynctask 使用默认的 java httpurlconnection 库从我的托管网页中获取数据,显然 doinbackground 方法没有在我的 android 9 设备上执行,但我可以在使用模拟器 api 24 测试时收到 json 响应。我还声明了互联网权限在清单中,我的 Firebase 工作正常,所以没有互联网问题。这种情况有什么解释吗?在继续使用带有 httpurlconnection 的 asynctask 时有什么解决方案吗?请帮忙,我必须在两天内展示我的应用程序。

sendDataAsync.class

package com.example.control;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;

import java.net.HttpURLConnection;
import java.net.URL;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

import android.os.AsyncTask;

class sendDataAsync extends AsyncTask<String , Void , String>{

            private AsyncResponse delegate = null;
            private String serverURL, responsemsg = "Not connected to host",entries;

            public sendDataAsync(String name, String prename, String email, String gender, String password, String cfirmpassword) {
                entries  = "name="+name+"&prename="+prename+"&email="+email+"&gender="+gender+"&password="+password+"&cfirmpassword="+cfirmpassword;
                serverURL = "http://iotcontrol.atwebpages.com/Register.php";
            }

            public sendDataAsync(String email, String password) {
                entries  = "password="+password+"&email="+email;
                serverURL = "http://iotcontrol.atwebpages.com/Login.php";
            }

            public void setDelegate(AsyncResponse del){
                this.delegate = del;
            }

            @Override
            protected String doInBackground(String... params) {
                try {
                    //entries = URLEncoder.encode(entries,"utf-8");
                    URL url = new URL(serverURL);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setReadTimeout(10000);
                    conn.setConnectTimeout(15000);
                    conn.setRequestMethod("POST");
                    conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded");
                    conn.setRequestProperty( "charset", "utf-8");
                    conn.setDoInput(true);
                    conn.setDoOutput(true);

                    OutputStream os = conn.getOutputStream();

                    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8));
                    writer.write(entries);
                    writer.flush();
                    writer.close();
                    os.close();

                    InputStream is = conn.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
                    String inputLine;
                    StringBuilder sb = new StringBuilder();
                    while ((inputLine = reader.readLine()) != null) {
                        sb.append(inputLine);
                    }
                    reader.close();
                    is.close();
                    conn.disconnect();
                    responsemsg = sb.toString();

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return responsemsg;
            }

            @Override
            protected void onPostExecute(String result) {
                delegate.processFinish(result);
            }

        }

在我的活动中,我有这个

  public void insertData() {
    sendDataAsync sendPostReqAsyncTask = new sendDataAsync(nameValue, prenameValue, emailValue, genderValue,passwordValue,cfirmpasswordValue);
    sendPostReqAsyncTask.setDelegate(this);
    sendPostReqAsyncTask.execute();
  }
/*
for some reason i cant access logcat so i just have a textview and print the response to it

and while on the emulator api24 i have the text view: {"ok":false,"message":"invalide data"}
but on my android 9 phone i have the text: Not connected to host
*/
  public void processFinish(String output) {
    TextView txt = (TextView)findViewById(R.id.textView3);
    txt.setText(output);
  }

标签: javaandroid

解决方案


从 Android 9 开始,默认情况下不允许使用 http 等明文流量。

如果服务器支持它,请改用 https。

如果服务器不支持 https,您需要专门选择加入明文流量。您可以使用清单在应用程序范围内执行此操作,但最好仅对具有网络安全配置的一台主机启用它。


推荐阅读