<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sam.web.webserviceapp">
<uses-permission android:name="androi" />
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sam.web.webserviceapp">
<uses-permission android:name="androi" />
https://www.youtube.com/watch?v=E5nsNrAWmLw
AndroidManifest.xml
=====================
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sam.web.webserviceapp">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:name=".AppController"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
==================================================================================
AppController.java
===================
package com.sam.web.webserviceapp;
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 {
public static final String TAG = AppController.class.getSimpleName();
private RequestQueue mRequestQueue;
private static AppController mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized AppController getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
MainActivity.java
==================
package com.sam.web.webserviceapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
final String url="http://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22";
String maxTemp;
String minTemp;
TextView max;
TextView min;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
max=(TextView) findViewById(R.id.maxValueTV);
min=(TextView) findViewById(R.id.minValueTV);
showService();
}
private void showService(){
/* StringRequest stringRequest=new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject=new JSONObject(response);
JSONObject main=jsonObject.getJSONObject("main");
maxTemp=main.getString("temp_max");
minTemp=main.getString("temp_min");
max.setText(maxTemp);
min.setText(minTemp);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getBaseContext(),error.toString(),Toast.LENGTH_LONG).show();
}
}); */
JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.GET, url, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject main=response.getJSONObject("main");
maxTemp=main.getString("temp_max");
minTemp=main.getString("temp_min");
max.setText(maxTemp);
min.setText(minTemp);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
AppController.getInstance().addToRequestQueue(jsonObjectRequest);
}
}
=====================================
activity_main.xml
===================
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.webserviceapp.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Max Temp"
android:id="@+id/maxTitleTV"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Min Temp"
android:id="@+id/minTitleTV"
android:layout_marginTop="10dp"
android:layout_below="@+id/maxTitleTV"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/maxValueTV"
android:text="max"
android:textSize="15sp"
android:textColor="#2291ff"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/maxTitleTV"
android:layout_alignBottom="@+id/maxTitleTV"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/minValueTV"
android:text="min"
android:textSize="15sp"
android:textColor="#2291ff"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/minTitleTV"
android:layout_alignBottom="@+id/minTitleTV"
/>
</RelativeLayout>