Volley Not making request on latest version of Android
Asked Answered
A

4

8

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

E/RecyclerView: No adapter attached; skipping layout

My code is

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!

Arber answered 14/1, 2019 at 5:8 Comment(3)
may be your are getting network security error. any error you are getting?Westmorland
Why do you think this error has anything to do with Volley?Stumble
Any Errors in logcat?Jung
D
29

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

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) 

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.

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>

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.

Dextrosinistral answered 14/1, 2019 at 5:39 Comment(5)
Thanks so much! I wasn't aware of the TLS by default, and much rather have it that way anyways. I just set up certbot on the server to fix that. I appreciate the info!Arber
Thank you so much!Historical
Ahh...saved my day!Ideality
Nice answer with explaination. Working fineCorky
What happened if this is ip address only?Arborization
M
6

It is working well in Pie Api 28

network_security_config.xml:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
    </base-config>
</network-security-config>

AndroidManifest.xml

In the android manifest Application Tag, add these lines.

 android:networkSecurityConfig="@xml/network_security_config"
 android:usesCleartextTraffic="true"
Malony answered 1/5, 2019 at 10:50 Comment(0)
R
4

Or you can use usesCleartextTraffic attribute under the application element in the android manifest. The default value in Android P is “false”.

<application
     android:usesCleartextTraffic="true"

</application>

but it requires minSdkVersion 23 or higher.

Reactivate answered 18/10, 2019 at 11:4 Comment(1)
nice trick!!! it is working for xampp localhost also, because it is http. much thanksToronto
L
1

For this issue i use https and its working very well now

Lakenyalaker answered 29/3, 2019 at 20:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.