Skip to content

Instantly share code, notes, and snippets.

@VerizonMediaOwner
Last active March 4, 2019 22:53
Show Gist options
  • Save VerizonMediaOwner/b5332cfd3bc33547d3d7d5c9b90d2411 to your computer and use it in GitHub Desktop.
Save VerizonMediaOwner/b5332cfd3bc33547d3d7d5c9b90d2411 to your computer and use it in GitHub Desktop.
Yahoo Weather API Android Example
// Copyright 2019 Oath Inc. Licensed under the terms of the zLib license see https://opensource.org/licenses/Zlib for terms.
import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Response;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.JsonRequest;
import com.google.gson.JsonSyntaxException;
import net.oauth.OAuth;
import net.oauth.OAuthAccessor;
import net.oauth.OAuthConsumer;
import net.oauth.OAuthException;
import net.oauth.OAuthMessage;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
public class ExampleRequest<T> extends JsonRequest<T> {
final String appId = "test-app-id";
final String CONSUMER_KEY = "your-consumer-key";
final String CONSUMER_SECRET = "your-consumer-secret";
final String baseUrl = "https://weather-ydn-yql.media.yahoo.com/forecastrss";
public ExampleRequest(int method, String url, String requestBody, Response.Listener<T> listener, Response.ErrorListener errorListener) {
super(method, url, requestBody, listener, errorListener);
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
OAuthConsumer consumer = new OAuthConsumer(null, CONSUMER_KEY, CONSUMER_SECRET, null);
consumer.setProperty(OAuth.OAUTH_SIGNATURE_METHOD, OAuth.HMAC_SHA1);
OAuthAccessor accessor = new OAuthAccessor(consumer);
try {
OAuthMessage request = accessor.newRequestMessage(OAuthMessage.GET, getUrl(), null);
String authorization = request.getAuthorizationHeader(null);
headers.put("Authorization", authorization);
} catch (OAuthException e |IOException | URISyntaxException e) {
throw new AuthFailureError(e.getMessage());
}
headers.put("X-Yahoo-App-Id", appId);
headers.put("Content-Type", "application/json");
return headers;
}
@Override
public String getUrl() {
return baseUrl + "?location=sunnyvale,ca&format=json";
}
@Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
try {
String json = new String(
response.data,
HttpHeaderParser.parseCharset(response.headers));
T parsedResponse = parseResponse(json);
return Response.success(
parsedResponse,
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException | JsonSyntaxException e) {
return Response.error(new ParseError(e));
}
}
private T parseResponse(String jsonObject) {
return null; // Add response parsing here
}
}
// Copyright 2019 Oath Inc. Licensed under the terms of the zLib license see https://opensource.org/licenses/Zlib for terms.
import android.content.Context;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
public class ExampleRequestManager {
private static ExampleRequestManager sInstance;
Context mContext;
RequestQueue mRequestQueue;
public static synchronized ExampleRequestManager getInstance(Context context) {
if (sInstance == null) {
sInstance = new ExampleRequestManager(context);
}
return sInstance;
}
private ExampleRequestManager(Context context) {
mContext = context;
mRequestQueue = Volley.newRequestQueue(mContext);
}
public <T> void addToRequestQueue(Request<T> request) {
mRequestQueue.add(request);
}
}
// Copyright 2019 Oath Inc. Licensed under the terms of the zLib license see https://opensource.org/licenses/Zlib for terms.
@Override
public void onStart() {
ExampleRequestManager requestManager = ExampleRequestManager.getInstance(this);
ExampleRequest request = new ExampleRequest(Request.Method.GET, null, null, new Response.Listener() {
@Override
public void onResponse(Object response) {
// Add success logic here
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Add error handling here
}
});
requestManager.addToRequestQueue(request);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment