java - 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);
}
解决方案
推荐阅读
- c++ - 在不破坏现有基类的情况下向具有许多派生类的抽象基类添加新方法
- java - Jackson Json Wrap 现场级别
- html - 为什么 Alegreya 字体在某些浏览器中无法正确显示?
- node.js - 带有客户端加密值的 Mongoose Model.find()
- sqlalchemy - Flask-SQLAlchemy 通过 IAM 连接到 RDS-proxy
- azure - 如何为 Get-AzSubscription 传递特定值
- r - 在 Mambaforge 的 Snakemake 工作流程中使用 R
- angular - 在返回值之前等待 observable 在每个数组中完成
- python - Prolog 嵌套查询
- node.js - Dynamo DB 上的节点 JS 查询使用 IAM 角色永远挂起