mirror of
https://github.com/moparisthebest/android-app
synced 2024-08-13 16:53:46 -04:00
Fetch unread articles from API
This commit is contained in:
parent
7e8ff86a43
commit
d6a3425850
@ -14,6 +14,8 @@ import java.net.Authenticator;
|
|||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.PasswordAuthentication;
|
import java.net.PasswordAuthentication;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
|
||||||
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
@ -39,7 +41,6 @@ import android.os.Bundle;
|
|||||||
import android.provider.Browser;
|
import android.provider.Browser;
|
||||||
import android.text.Html;
|
import android.text.Html;
|
||||||
import android.util.Base64;
|
import android.util.Base64;
|
||||||
import android.util.Log;
|
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuInflater;
|
import android.view.MenuInflater;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
@ -55,7 +56,6 @@ import static fr.gaulupeau.apps.Poche.ArticlesSQLiteOpenHelper.ARTICLE_URL;
|
|||||||
import static fr.gaulupeau.apps.Poche.ArticlesSQLiteOpenHelper.ARTICLE_TITLE;
|
import static fr.gaulupeau.apps.Poche.ArticlesSQLiteOpenHelper.ARTICLE_TITLE;
|
||||||
import static fr.gaulupeau.apps.Poche.ArticlesSQLiteOpenHelper.ARTICLE_CONTENT;
|
import static fr.gaulupeau.apps.Poche.ArticlesSQLiteOpenHelper.ARTICLE_CONTENT;
|
||||||
import static fr.gaulupeau.apps.Poche.ArticlesSQLiteOpenHelper.ARCHIVE;
|
import static fr.gaulupeau.apps.Poche.ArticlesSQLiteOpenHelper.ARCHIVE;
|
||||||
import static fr.gaulupeau.apps.Poche.Helpers.getInputStreamFromUrl;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main activity class
|
* Main activity class
|
||||||
@ -66,6 +66,12 @@ import static fr.gaulupeau.apps.Poche.Helpers.getInputStreamFromUrl;
|
|||||||
Button btnGetPost;
|
Button btnGetPost;
|
||||||
Button btnSync;
|
Button btnSync;
|
||||||
EditText editPocheUrl;
|
EditText editPocheUrl;
|
||||||
|
SharedPreferences settings;
|
||||||
|
String globalToken;
|
||||||
|
String apiUsername;
|
||||||
|
String apiToken;
|
||||||
|
String pocheUrl;
|
||||||
|
|
||||||
/** Called when the activity is first created.
|
/** Called when the activity is first created.
|
||||||
* Will act differently depending on whether sharing or
|
* Will act differently depending on whether sharing or
|
||||||
* displaying information page. */
|
* displaying information page. */
|
||||||
@ -75,11 +81,11 @@ import static fr.gaulupeau.apps.Poche.Helpers.getInputStreamFromUrl;
|
|||||||
Intent intent = getIntent();
|
Intent intent = getIntent();
|
||||||
Bundle extras = intent.getExtras();
|
Bundle extras = intent.getExtras();
|
||||||
String action = intent.getAction();
|
String action = intent.getAction();
|
||||||
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
|
settings = getSharedPreferences(PREFS_NAME, 0);
|
||||||
final String pocheUrl = settings.getString("pocheUrl", "http://");
|
pocheUrl = settings.getString("pocheUrl", "http://");
|
||||||
final String globalToken = settings.getString("globalToken", "");
|
globalToken = settings.getString("globalToken", "");
|
||||||
final String apiUsername = settings.getString("APIUsername", "");
|
apiUsername = settings.getString("APIUsername", "");
|
||||||
final String apiToken = settings.getString("APIToken", "");
|
apiToken = settings.getString("APIToken", "");
|
||||||
|
|
||||||
// Find out if Sharing or if app has been launched from icon
|
// Find out if Sharing or if app has been launched from icon
|
||||||
if (action.equals(Intent.ACTION_SEND) && pocheUrl != "http://") {
|
if (action.equals(Intent.ACTION_SEND) && pocheUrl != "http://") {
|
||||||
@ -123,64 +129,7 @@ import static fr.gaulupeau.apps.Poche.Helpers.getInputStreamFromUrl;
|
|||||||
// Exécution de la synchro en arrière-plan
|
// Exécution de la synchro en arrière-plan
|
||||||
new Thread(new Runnable() {
|
new Thread(new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
String id = "req-001";
|
fetchUnread();
|
||||||
JSONRPC2Request reqOut = null;
|
|
||||||
try {
|
|
||||||
// POCHE A LINK
|
|
||||||
//reqOut = JSONRPC2Request.parse("{\"jsonrpc\":\"2.0\",\"method\":\"item.add\",\"id\":\"req-001\",\"params\":[{\"username\":\"poche\",\"api_token\":\"cPG2urVgA+ToMXY\"},\"http://cdetc.fr\",true]}");
|
|
||||||
// GET A LINK
|
|
||||||
reqOut = JSONRPC2Request.parse("{\"jsonrpc\":\"2.0\",\"method\":\"item.info\",\"id\":\"" + id + "\",\"params\":[{\"username\":\""+ apiUsername + "\",\"api_token\":\""+ apiToken +"\"}, 1]}");
|
|
||||||
} catch (JSONRPC2ParseException e2) {
|
|
||||||
e2.printStackTrace();
|
|
||||||
}
|
|
||||||
System.out.println(reqOut.toString());
|
|
||||||
URL url = null;
|
|
||||||
try {
|
|
||||||
final String rpcuser ="api_user";
|
|
||||||
final String rpcpassword = globalToken;
|
|
||||||
|
|
||||||
Authenticator.setDefault(new Authenticator() {
|
|
||||||
protected PasswordAuthentication getPasswordAuthentication() {
|
|
||||||
return new PasswordAuthentication (rpcuser, rpcpassword.toCharArray());
|
|
||||||
}});
|
|
||||||
url = new URL(pocheUrl + "/jsonrpc.php");
|
|
||||||
} catch (MalformedURLException e1) {
|
|
||||||
e1.printStackTrace();
|
|
||||||
}
|
|
||||||
JSONRPC2Session session = new JSONRPC2Session(url);
|
|
||||||
JSONRPC2Response response = null;
|
|
||||||
try{
|
|
||||||
response = session.send(reqOut);
|
|
||||||
} catch (JSONRPC2SessionException e) {
|
|
||||||
|
|
||||||
System.err.println(e.getMessage());
|
|
||||||
}
|
|
||||||
if (response.indicatesSuccess()){
|
|
||||||
JSONObject article = null;
|
|
||||||
ContentValues values = new ContentValues();
|
|
||||||
try {
|
|
||||||
article = new JSONObject(response.getResult().toString());
|
|
||||||
values.put(ARTICLE_TITLE, Html.fromHtml(article.getString("title")).toString());
|
|
||||||
values.put(ARTICLE_CONTENT, Html.fromHtml(article.getString("content")).toString());
|
|
||||||
values.put(ARTICLE_ID, Html.fromHtml(article.getString("id")).toString());
|
|
||||||
values.put(ARTICLE_URL, Html.fromHtml(article.getString("url")).toString());
|
|
||||||
values.put(ARCHIVE, 0);
|
|
||||||
} catch (JSONException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
showToast(getString(R.string.txtSyncFailed));
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
database.insertOrThrow(ARTICLE_TABLE, null, values);
|
|
||||||
} catch (SQLiteConstraintException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
showToast(getString(R.string.txtSyncFailed));
|
|
||||||
}
|
|
||||||
showToast(getString(R.string.txtSyncDone));
|
|
||||||
updateUnread();
|
|
||||||
}else{
|
|
||||||
System.out.println(response.getError().getMessage( ));
|
|
||||||
showToast(getString(R.string.txtSyncFailed));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}).start();
|
}).start();
|
||||||
} else {
|
} else {
|
||||||
@ -227,6 +176,12 @@ import static fr.gaulupeau.apps.Poche.Helpers.getInputStreamFromUrl;
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onDestroy() {
|
||||||
|
super.onDestroy();
|
||||||
|
database.close();
|
||||||
|
}
|
||||||
|
|
||||||
private void updateUnread(){
|
private void updateUnread(){
|
||||||
runOnUiThread(new Runnable() {
|
runOnUiThread(new Runnable() {
|
||||||
public void run()
|
public void run()
|
||||||
@ -248,5 +203,72 @@ import static fr.gaulupeau.apps.Poche.Helpers.getInputStreamFromUrl;
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void fetchUnread(){
|
||||||
|
String id = "req-001";
|
||||||
|
JSONRPC2Request reqOut = null;
|
||||||
|
try {
|
||||||
|
// POCHE A LINK
|
||||||
|
//reqOut = JSONRPC2Request.parse("{\"jsonrpc\":\"2.0\",\"method\":\"item.add\",\"id\":\"req-001\",\"params\":[{\"username\":\"poche\",\"api_token\":\"cPG2urVgA+ToMXY\"},\"http://cdetc.fr\",true]}");
|
||||||
|
// GET A LINK
|
||||||
|
//reqOut = JSONRPC2Request.parse("{\"jsonrpc\":\"2.0\",\"method\":\"item.info\",\"id\":\"" + id + "\",\"params\":[{\"username\":\""+ apiUsername + "\",\"api_token\":\""+ apiToken +"\"}, 1]}");
|
||||||
|
// GET ALL UNREAD
|
||||||
|
reqOut = JSONRPC2Request.parse("{\"jsonrpc\":\"2.0\",\"method\":\"item.list_unread\",\"id\":\"" + id + "\",\"params\":[{\"username\":\""+ apiUsername + "\",\"api_token\":\""+ apiToken +"\"}, null, null]}");
|
||||||
|
System.err.println(reqOut.toString());
|
||||||
|
} catch (JSONRPC2ParseException e2) {
|
||||||
|
e2.printStackTrace();
|
||||||
|
}
|
||||||
|
System.out.println(reqOut.toString());
|
||||||
|
URL url = null;
|
||||||
|
try {
|
||||||
|
final String rpcuser ="api_user";
|
||||||
|
final String rpcpassword = globalToken;
|
||||||
|
|
||||||
|
Authenticator.setDefault(new Authenticator() {
|
||||||
|
protected PasswordAuthentication getPasswordAuthentication() {
|
||||||
|
return new PasswordAuthentication (rpcuser, rpcpassword.toCharArray());
|
||||||
|
}});
|
||||||
|
url = new URL(pocheUrl + "/jsonrpc.php");
|
||||||
|
} catch (MalformedURLException e1) {
|
||||||
|
e1.printStackTrace();
|
||||||
|
}
|
||||||
|
JSONRPC2Session session = new JSONRPC2Session(url);
|
||||||
|
JSONRPC2Response response = null;
|
||||||
|
try{
|
||||||
|
response = session.send(reqOut);
|
||||||
|
} catch (JSONRPC2SessionException e) {
|
||||||
|
|
||||||
|
System.err.println(e.getMessage());
|
||||||
|
}
|
||||||
|
if (response.indicatesSuccess()){
|
||||||
|
JSONObject article = null;
|
||||||
|
ContentValues values = new ContentValues();
|
||||||
|
try {
|
||||||
|
JSONArray ret = new JSONArray(response.getResult().toString());
|
||||||
|
for (int i = 0; i < ret.length(); i++) {
|
||||||
|
article = ret.getJSONObject(i);
|
||||||
|
values.put(ARTICLE_TITLE, Html.fromHtml(article.getString("title")).toString());
|
||||||
|
values.put(ARTICLE_CONTENT, Html.fromHtml(article.getString("content")).toString());
|
||||||
|
values.put(ARTICLE_ID, Html.fromHtml(article.getString("id")).toString());
|
||||||
|
values.put(ARTICLE_URL, Html.fromHtml(article.getString("url")).toString());
|
||||||
|
values.put(ARCHIVE, 0);
|
||||||
|
try {
|
||||||
|
database.insertOrThrow(ARTICLE_TABLE, null, values);
|
||||||
|
} catch (SQLiteConstraintException e) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (JSONException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
showToast(getString(R.string.txtSyncFailed));
|
||||||
|
}
|
||||||
|
|
||||||
|
showToast(getString(R.string.txtSyncDone));
|
||||||
|
updateUnread();
|
||||||
|
}else{
|
||||||
|
System.out.println(response.getError().getMessage( ));
|
||||||
|
showToast(getString(R.string.txtSyncFailed));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -58,4 +58,10 @@ public class ReadArticle extends Activity {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onDestroy() {
|
||||||
|
super.onDestroy();
|
||||||
|
database.close();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user