问题描述
我注意到我的应用程序无法在Pixel 3上运行.我进入android studio并模拟了一些设备,并注意到它不适用于任何Android Pie(API 28)设备,但在其他任何设备上都可以正常工作.我在代码中放了一些日志,看来我在Volley使用中没有得到响应.该应用程序本身已加载,但未显示任何数据,并且出现错误
I noticed my app wasn't working on Pixel 3. I went into android studio and emulated a few devices and noticed it is not working on any Android Pie (API 28) devices, but works fine on any other. I put a few logs throughout my code, and it seemed that I was not getting a response within my Volley usage. The app itself loads, but does not display any data, and I receive an error
我的代码是
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
DatabaseManagement db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
db = new DatabaseManagement(this);
grabData();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(MainActivity.this, com.hellapunk.hellapunk.feature.Main2Activity.class);
startActivity(i);
finish();
}
}, 3000);
}
public void grabData() {
//Log.i("Made it", "You made it this far");
String url = "http://hellapunk.com/listallshows.php?id=2018";
StringRequest sr = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray showInfo = new JSONArray(response);
Log.i("Something", "response");
for (int i = 0; i < showInfo.length(); ++i) {
JSONObject showInfo1 = showInfo.getJSONObject(i);
if ((db.checkShow(showInfo1.getString("show_summary"),
showInfo1.getString("show_date"))) > 0) {
Log.i("Results", "Show already exists");
} else {
db.insertShows(showInfo1.getString("show_summary"),
showInfo1.getString("venue_name"),
showInfo1.getString("show_date"),
showInfo1.getString("shows_img"),
showInfo1.getString("venue_address"),
showInfo1.getString("city_name"),
showInfo1.getString("city_region"));
}
}
} catch (Exception e) {
Log.i("Error", e.getMessage());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("TAG", ""+error);
}
});
Volley.newRequestQueue(this).add(sr);
}
}
我在启动画面中获取数据并将其存储到本地数据库中.然后在下一个活动中从数据库中调用它.任何建议将不胜感激!
I grab the data during my splash screen and store it into a local database. Then it is called from the db on the next Activity. Any advice will be appreciated!
推荐答案
这是因为从Android P开始,您的应用程序和不安全的目标之间的任何网络流量都必须明确列入白名单.请参阅默认情况下使用TLS保护用户在Android P 中.
This is because starting with Android P, any network traffic between your app and insecure destinations must be explicitly whitelisted. See Protecting users with TLS by default in Android P.
在您的代码中,您正在请求:
In your code, you're making a request to:
http://hellapunk.com/listallshows.php?id=2018
http://
显示该站点不安全.如果您更深入地研究LogCat,您可能会发现一条消息,例如:
The http://
shows that the site is not secure. If you dig deeper into your LogCat you'll likely find a message such as:
com.android.volley.NoConnectionError: java.io.IOException: Cleartext HTTP traffic to hellapunk.com not permitted
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:177)
at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:120)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:87)
Caused by: java.io.IOException: Cleartext HTTP traffic to hellapunk.com not permitted
at com.android.okhttp.HttpHandler$CleartextURLFilter.checkURLPermitted(HttpHandler.java:115)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:458)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:538)
at com.android.volley.toolbox.HurlStack.executeRequest(HurlStack.java:99)
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:131)
at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:120)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:87)
我自己尝试过,该站点似乎不支持https
连接,因此,为了从您的应用程序访问此特定服务器,您需要手动将hellapunk.com
域列入白名单.
Attempting it myself, it doesn't appear that the site supports https
connections, so in order to reach this particular server from your application, you would need to whitelist the hellapunk.com
domain manually.
在资源目录中,为您的网络安全配置定义XML文档(例如res/xml/network_security_config.xml
):
In your resources directory, define an XML document for your network security configuration (e.g. res/xml/network_security_config.xml
):
network_security_config.xml :
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">hellapunk.com</domain>
</domain-config>
</network-security-config>
然后,在应用程序的AndroidManifest.xml
中的<application>
标记中,添加属性:
Then, in your AndroidManifest.xml
for your application, in the <application>
tag, add the attribute:
<application
android:networkSecurityConfig="@xml/network_security_config"
然后应该允许您向该文件内指定的任何域提出不安全的请求.
You should then be allowed to make insecure requests to any domain specified within that file.
这篇关于Volley未在最新版本的Android上发出请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!