0% found this document useful (0 votes)
27 views4 pages

Assignment 12 MC 007

Uploaded by

sujata
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views4 pages

Assignment 12 MC 007

Uploaded by

sujata
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

SIES College of Management Studies SYMCA (Revised), Sem III,

Roll No: 07

Assignment - 12

Create students record with 4 members rollno, name, age, gender. Use Volley library to
retrieve the records from the network. (Use JSONArrayRequest)
Code:
MainActivity.java:
package com.example.p12;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {


Button ret;
private String url = "https://mocki.io/v1/a1e5516e-c3c3-43e9-a012-d93def437f15";
private JsonArrayRequest jsonreq;
private RequestQueue queue;

TextView tvData;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ret = findViewById(R.id.btn_fetch);
tvData = findViewById(R.id.tvData);
ret.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
queue = Volley.newRequestQueue(getApplicationContext());
jsonreq = new JsonArrayRequest(Request.Method.GET, url, null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Subject: MCAL34 Mobile Computing Lab Academic Year
First Half 2023_24
SIES College of Management Studies SYMCA (Revised), Sem III,
Roll No: 07

StringBuffer buffer = new StringBuffer();


JSONObject jsonObject = null;
for (int i = 0; i < response.length(); i++) {
try {
jsonObject = response.getJSONObject(i);
buffer.append("Roll No: "+jsonObject.getString("rollno")+"\n");
buffer.append("Name: "+jsonObject.getString("name")+"\n");
buffer.append("Age: "+jsonObject.getString("age")+"\n");
buffer.append("Gender: "+jsonObject.getString("gender")+"\n");
buffer.append("\n \n");
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
tvData.setText(buffer.toString());
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

Toast.makeText(getApplicationContext(),error.toString(),Toast.LENGTH_LONG).show();
}
});
queue.add(jsonreq);
}
});
}
}

AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.P12"
tools:targetApi="31">
<activity

Subject: MCAL34 Mobile Computing Lab Academic Year


First Half 2023_24
SIES College of Management Studies SYMCA (Revised), Sem III,
Roll No: 07

android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

build.gradle:
plugins {
id 'com.android.application'
}
android {
namespace 'com.example.p12'
compileSdk 33
defaultConfig {
applicationId "com.example.p12"
minSdk 26
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-
rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.10.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'com.android.volley:volley:1.2.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

Subject: MCAL34 Mobile Computing Lab Academic Year


First Half 2023_24
SIES College of Management Studies SYMCA (Revised), Sem III,
Roll No: 07

Output:

Subject: MCAL34 Mobile Computing Lab Academic Year


First Half 2023_24

You might also like