Get data from server using Volley library | Volley Library Tutorials
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Code From Learnoset - Learn Coding Online.-->
<!-- Download Learnoset App from PlayStore to learn coding,projects-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.learnoset.splashscreen">
<!-- Add internet permissions -->
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.SplashScreen">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
</application>
</manifest>
MainActivity.java
package com.learnoset.splashscreen;
/* Code From Learnoset - Learn Coding Online.*/
/* Download Learnoset App from PlayStore to learn coding,
* projects
*/
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url = "https://learnoset.com";
// make string request to server
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Do something with the response
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// An error occurred. you can handle errors here...
}
});
/* add the string request to request queue
* Volley library will pick this request from queue*/
MySingleton.getInstance(this).addToRequestQueue(stringRequest);
}
}
MySingleton.java
package com.learnoset.splashscreen;
/* Code From Learnoset - Learn Coding Online.*/
/* Download Learnoset App from PlayStore to learn coding,
* projects
*/
import android.content.Context;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
public class MySingleton {
private static MySingleton instance;
private static Context ctx;
private RequestQueue requestQueue;
private MySingleton(Context context) {
ctx = context;
// create a new request queue to hold requests into a queue
requestQueue = getRequestQueue();
}
public static synchronized MySingleton getInstance(Context context) {
if (instance == null) {
instance = new MySingleton(context);
}
return instance;
}
public RequestQueue getRequestQueue() {
if (requestQueue == null) {
// getApplicationContext() is key, it keeps you from leaking the
// Activity or BroadcastReceiver if someone passes one in.
requestQueue = Volley.newRequestQueue(ctx.getApplicationContext());
}
return requestQueue;
}
public <T> void addToRequestQueue(Request<T> req) {
getRequestQueue().add(req);
}
}
Projects with Source Code + Video Tutorials
You can download our Java and Android Studio Projects with Source Code and Video Tutorials.
HD Wallpaper App in Android Studio
Simple Music Player App (Version 2) for Android (Offline Version)
Modern Login and Register screen UI design
If you have any Questions or Queries
You can mail us at info.learnoset@gmail.com
Follow us to learn Coding and get in touch with new Technologies.