mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-29 13:53:54 +01:00
Wrote Listener classes and request method
This commit is contained in:
parent
4b4356b0f0
commit
021c17166a
2 changed files with 144 additions and 3 deletions
|
|
@ -1,17 +1,147 @@
|
|||
package fr.free.nrw.commons.upload;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.volley.Cache;
|
||||
import com.android.volley.NetworkResponse;
|
||||
import com.android.volley.Request;
|
||||
import com.android.volley.RequestQueue;
|
||||
import com.android.volley.Response;
|
||||
import com.android.volley.toolbox.HttpHeaderParser;
|
||||
import com.android.volley.toolbox.JsonRequest;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
|
||||
public class MwVolley {
|
||||
|
||||
public MwVolley(Context context) {
|
||||
private Context context;
|
||||
private String coords;
|
||||
private static final String MWURL = "https://commons.wikimedia.org/";
|
||||
private String apiUrl;
|
||||
private int radius;
|
||||
private Gson GSON;
|
||||
|
||||
protected Set<String> categorySet;
|
||||
|
||||
public MwVolley(Context context, String coords) {
|
||||
|
||||
this.context = context;
|
||||
this.coords = coords;
|
||||
categorySet = new HashSet<String>();
|
||||
GSON = new GsonBuilder().create();
|
||||
//Instantiate RequestQueue with Application context
|
||||
RequestQueue queue = VolleyRequestQueue.getInstance(this.getApplicationContext()).getRequestQueue();
|
||||
RequestQueue queue = VolleyRequestQueue.getInstance(context.getApplicationContext()).getRequestQueue();
|
||||
}
|
||||
|
||||
protected void setRadius(int radius) {
|
||||
this.radius = radius;
|
||||
}
|
||||
|
||||
protected int getRadius() {
|
||||
return radius;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds URL with image coords for MediaWiki API calls
|
||||
* Example URL: https://commons.wikimedia.org/w/api.php?action=query&prop=categories|coordinates|pageprops&format=json&clshow=!hidden&coprop=type|name|dim|country|region|globe&codistancefrompoint=38.11386944444445|13.356263888888888&
|
||||
* generator=geosearch&redirects=&ggscoord=38.11386944444445|13.356263888888888&ggsradius=100&ggslimit=10&ggsnamespace=6&ggsprop=type|name|dim|country|region|globe&ggsprimary=all&formatversion=2
|
||||
*/
|
||||
private String buildUrl(int ggsradius) {
|
||||
|
||||
Uri.Builder builder = Uri.parse(MWURL).buildUpon();
|
||||
|
||||
builder.appendPath("w")
|
||||
.appendPath("api.php")
|
||||
.appendQueryParameter("action", "query")
|
||||
.appendQueryParameter("prop", "categories|coordinates|pageprops")
|
||||
.appendQueryParameter("format", "json")
|
||||
.appendQueryParameter("clshow", "!hidden")
|
||||
.appendQueryParameter("coprop", "type|name|dim|country|region|globe")
|
||||
.appendQueryParameter("codistancefrompoint", coords)
|
||||
.appendQueryParameter("generator", "geosearch")
|
||||
.appendQueryParameter("ggscoord", coords)
|
||||
.appendQueryParameter("ggsradius", Integer.toString(ggsradius))
|
||||
.appendQueryParameter("ggslimit", "10")
|
||||
.appendQueryParameter("ggsnamespace", "6")
|
||||
.appendQueryParameter("ggsprop", "type|name|dim|country|region|globe")
|
||||
.appendQueryParameter("ggsprimary", "all")
|
||||
.appendQueryParameter("formatversion", "2");
|
||||
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
public void request() {
|
||||
|
||||
radius = getRadius();
|
||||
apiUrl = buildUrl(radius);
|
||||
|
||||
ShareActivity.ResponseListener responseListener = new ShareActivity.ResponseListener();
|
||||
ShareActivity.ErrorListener errorListener = new ShareActivity.ErrorListener();
|
||||
|
||||
JsonRequest request = new QueryRequest(apiUrl, responseListener, errorListener);
|
||||
VolleyRequestQueue.getInstance(context).addToRequestQueue(request);
|
||||
}
|
||||
|
||||
private class QueryRequest extends JsonRequest<QueryResponse> {
|
||||
|
||||
public QueryRequest(String url, Response.Listener<QueryResponse> listener, Response.ErrorListener errorListener) {
|
||||
super(Request.Method.GET, url, null, listener, errorListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Response<QueryResponse> parseNetworkResponse(NetworkResponse response) {
|
||||
String json = parseString(response);
|
||||
QueryResponse queryResponse = GSON.fromJson(json, QueryResponse.class);
|
||||
return Response.success(queryResponse, cacheEntry(response));
|
||||
}
|
||||
|
||||
private Cache.Entry cacheEntry(NetworkResponse response) {
|
||||
return HttpHeaderParser.parseCacheHeaders(response);
|
||||
}
|
||||
|
||||
private String parseString(NetworkResponse response) {
|
||||
try {
|
||||
return new String(response.data, HttpHeaderParser.parseCharset(response.headers));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
return new String(response.data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class QueryResponse {
|
||||
private Query query = new Query();
|
||||
|
||||
private String printSet() {
|
||||
if (categorySet == null || categorySet.isEmpty()) {
|
||||
GpsCatExists.setGpsCatExists(false);
|
||||
Log.d("Cat", "gpsCatExists=" + GpsCatExists.getGpsCatExists());
|
||||
return "No collection of categories";
|
||||
} else {
|
||||
GpsCatExists.setGpsCatExists(true);
|
||||
Log.d("Cat", "gpsCatExists=" + GpsCatExists.getGpsCatExists());
|
||||
return "CATEGORIES FOUND" + categorySet.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (query != null) {
|
||||
return "query=" + query.toString() + "\n" + printSet();
|
||||
} else {
|
||||
return "No pages found";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import android.os.*;
|
|||
|
||||
import com.android.volley.RequestQueue;
|
||||
import com.android.volley.Response;
|
||||
import com.android.volley.VolleyError;
|
||||
import com.nostra13.universalimageloader.core.ImageLoader;
|
||||
import android.net.*;
|
||||
import android.support.v4.app.NavUtils;
|
||||
|
|
@ -210,13 +211,23 @@ public class ShareActivity
|
|||
requestAuthToken();
|
||||
}
|
||||
|
||||
private class ResponseListener<T> implements Response.Listener<T>{
|
||||
protected static class ResponseListener<T> implements Response.Listener<T>{
|
||||
@Override
|
||||
public void onResponse(T response){
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
protected static class ErrorListener implements Response.ErrorListener {
|
||||
|
||||
private static final String TAG = ErrorListener.class.getName();
|
||||
|
||||
@Override
|
||||
public void onErrorResponse(VolleyError error) {
|
||||
Log.e(TAG, error.toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue