Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Volley Not making request on latest version of Android

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!

like image 902
j_timko Avatar asked Jan 14 '19 05:01

j_timko


3 Answers

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.

like image 150
Kevin Coppock Avatar answered Oct 09 '22 09:10

Kevin Coppock


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"
like image 27
Rakesh Yadav Avatar answered Oct 09 '22 10:10

Rakesh Yadav


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.

like image 21
Aristo Michael Avatar answered Oct 09 '22 09:10

Aristo Michael