JSON-->java script object notation ---more human readable
https://www.youtube.com/watch?v=E5nsNrAWmLw
MainActivity.java
=====================
package com.sam.web.webservicedemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ListView listView;
String url="http://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22";
ArrayList<String> nameList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView) findViewById(R.id.list);
getPoetNames();
}
private void getPoetNames() {
JsonObjectRequest request=new JsonObjectRequest(Request.Method.GET, url, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray array=response.getJSONArray("weather");
for (int i=0; i<array.length();i++){
JSONObject object=array.getJSONObject(i);
String main=object.getString("main");
nameList.add(main);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
AppController.getInstance().addToRequestQueue(request);
ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,nameList);
listView.setAdapter(adapter);
}
}
=======================================================================
AppController.java
=========================
package com.sam.web.webservicedemo;
import android.app.Application;
import android.text.TextUtils;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
public class AppController extends Application {
private static AppController instance;
private RequestQueue requestQueue;
public static synchronized AppController getInstance(){
return instance;
}
@Override
public void onCreate() {
super.onCreate();
instance=this;
}
private RequestQueue getRequestQueue(){
if(requestQueue==null){
requestQueue=Volley.newRequestQueue(this);
}
return requestQueue;
}
public void addToRequestQueue(Request request){
getRequestQueue().add(request);
}
}
===============================================
activity_main.xml
==================
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.sam.web.webservicedemo.MainActivity">
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
thistimebd Bangladesh Live online newsportal, education, Lifestyle, Health, Photography, gif image etc.
Make your own name or company name website | contact: thistimebd24@gmail.com
Copyright © 2020-2024 News Portal in Bangladesh - THISTIMEBD.COM. ALL Rights Reserved.