mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 12:23:58 +01:00
Split title category code into class that extends AsyncTask
This commit is contained in:
parent
5a155f1de4
commit
01bd8845c2
2 changed files with 66 additions and 31 deletions
|
|
@ -127,38 +127,8 @@ public class CategorizationFragment extends Fragment {
|
|||
String title = titleDesc.getString("Title", "");
|
||||
Log.d(TAG, "Title: " + title);
|
||||
|
||||
//TODO: Split this into AsyncTask
|
||||
//TODO: Add call to TitleCategories AsyncTask here
|
||||
|
||||
MWApi api = CommonsApplication.createMWApi();
|
||||
ApiResult result;
|
||||
|
||||
//URL https://commons.wikimedia.org/w/api.php?action=query&format=xml&list=search&srwhat=text&srenablerewrites=1&srnamespace=14&srlimit=10&srsearch=
|
||||
try {
|
||||
result = api.action("query")
|
||||
.param("format", "xml")
|
||||
.param("list", "search")
|
||||
.param("srwhat", "text")
|
||||
.param("srnamespace", "14")
|
||||
.param("srlimit", SEARCH_CATS_LIMIT)
|
||||
.param("srsearch", title)
|
||||
.get();
|
||||
Log.d(TAG, "Searching for cats for title: " + result.toString());
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "IO Exception: ", e);
|
||||
//Return empty arraylist
|
||||
return items;
|
||||
}
|
||||
|
||||
ArrayList<ApiResult> categoryNodes = result.getNodes("/api/query/search/p/@title");
|
||||
for(ApiResult categoryNode: categoryNodes) {
|
||||
String cat = categoryNode.getDocument().getTextContent();
|
||||
String catString = cat.replace("Category:", "");
|
||||
items.add(catString);
|
||||
}
|
||||
|
||||
Log.d(TAG, "Title cat query results: " + items);
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -0,0 +1,65 @@
|
|||
package fr.free.nrw.commons.category;
|
||||
|
||||
import android.os.AsyncTask;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
|
||||
import org.mediawiki.api.ApiResult;
|
||||
import org.mediawiki.api.MWApi;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import fr.free.nrw.commons.CommonsApplication;
|
||||
|
||||
/**
|
||||
* Created by misao on 11-Oct-16.
|
||||
*/
|
||||
|
||||
public class TitleCategories extends AsyncTask<Void, Void, ArrayList<String>> {
|
||||
|
||||
@Override
|
||||
protected void onPreExecute() {
|
||||
super.onPreExecute();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ArrayList<String> doInBackground(Void... voids) {
|
||||
|
||||
|
||||
//TODO: Split this into AsyncTask
|
||||
|
||||
MWApi api = CommonsApplication.createMWApi();
|
||||
ApiResult result;
|
||||
|
||||
//URL https://commons.wikimedia.org/w/api.php?action=query&format=xml&list=search&srwhat=text&srenablerewrites=1&srnamespace=14&srlimit=10&srsearch=
|
||||
try {
|
||||
result = api.action("query")
|
||||
.param("format", "xml")
|
||||
.param("list", "search")
|
||||
.param("srwhat", "text")
|
||||
.param("srnamespace", "14")
|
||||
.param("srlimit", SEARCH_CATS_LIMIT)
|
||||
.param("srsearch", title)
|
||||
.get();
|
||||
Log.d(TAG, "Searching for cats for title: " + result.toString());
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "IO Exception: ", e);
|
||||
//Return empty arraylist
|
||||
return items;
|
||||
}
|
||||
|
||||
ArrayList<ApiResult> categoryNodes = result.getNodes("/api/query/search/p/@title");
|
||||
for(ApiResult categoryNode: categoryNodes) {
|
||||
String cat = categoryNode.getDocument().getTextContent();
|
||||
String catString = cat.replace("Category:", "");
|
||||
items.add(catString);
|
||||
}
|
||||
|
||||
Log.d(TAG, "Title cat query results: " + items);
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue