Start to use poche API.

This commit is contained in:
GAULUPEAU Jonathan 2013-11-15 14:32:25 +01:00
parent 3630531c44
commit 7e8ff86a43
8 changed files with 150 additions and 65 deletions

BIN
libs/json-smart-1.2.jar Normal file

Binary file not shown.

BIN
libs/jsonrpc2-base-1.35.jar Normal file

Binary file not shown.

Binary file not shown.

View File

@ -41,6 +41,51 @@
<requestFocus /> <requestFocus />
</EditText> </EditText>
<TextView
android:id="@+id/txtGlobalToken"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/txtGlobalToken" />
<EditText
android:id="@+id/globalToken"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="text" />
<TextView
android:id="@+id/txtAPIUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/txtAPIUsername" />
<EditText
android:id="@+id/APIUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="text" />
<TextView
android:id="@+id/txtAPIToken"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/txtAPIToken" />
<EditText
android:id="@+id/APIToken"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="text" />
<TextView <TextView
android:id="@+id/instructions" android:id="@+id/instructions"
android:layout_width="wrap_content" android:layout_width="wrap_content"

View File

@ -18,4 +18,7 @@
<string name="txtSyncDone">Synchronize done !</string> <string name="txtSyncDone">Synchronize done !</string>
<string name="txtSyncFailed">Synchronize failed !</string> <string name="txtSyncFailed">Synchronize failed !</string>
<string name="txtNetOffline">Check Internet Connectivity !</string> <string name="txtNetOffline">Check Internet Connectivity !</string>
<string name="txtGlobalToken">API global token :</string>
<string name="txtAPIUsername">Your API username :</string>
<string name="txtAPIToken">Your API token :</string>
</resources> </resources>

View File

@ -56,17 +56,6 @@ public class ArticlesSQLiteOpenHelper extends SQLiteOpenHelper {
"UNIQUE (" + ARTICLE_ID + ")" + "UNIQUE (" + ARTICLE_ID + ")" +
");" ");"
); );
db.execSQL(
"INSERT INTO " + ARTICLE_TABLE +
" (" +
ARTICLE_TITLE + ", " +
ARTICLE_CONTENT + ", " +
ARTICLE_ID + ", " +
ARTICLE_URL + ", " +
ARCHIVE +
") VALUES (" +
"'Ceci est un test', 'coucou', 1, 'toto', 0);"
);
} }

View File

@ -10,11 +10,19 @@ package fr.gaulupeau.apps.Poche;
import fr.gaulupeau.apps.InThePoche.R; import fr.gaulupeau.apps.InThePoche.R;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.Authenticator;
import org.json.JSONArray; import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import com.thetransactioncompany.jsonrpc2.JSONRPC2ParseException;
import com.thetransactioncompany.jsonrpc2.JSONRPC2Request;
import com.thetransactioncompany.jsonrpc2.JSONRPC2Response;
import com.thetransactioncompany.jsonrpc2.client.JSONRPC2Session;
import com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.app.Activity; import android.app.Activity;
import android.content.ContentValues; import android.content.ContentValues;
@ -31,6 +39,7 @@ 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;
@ -67,7 +76,10 @@ import static fr.gaulupeau.apps.Poche.Helpers.getInputStreamFromUrl;
Bundle extras = intent.getExtras(); Bundle extras = intent.getExtras();
String action = intent.getAction(); String action = intent.getAction();
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String pocheUrl = settings.getString("pocheUrl", "http://"); final String pocheUrl = settings.getString("pocheUrl", "http://");
final String globalToken = settings.getString("globalToken", "");
final String apiUsername = settings.getString("APIUsername", "");
final String 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://") {
@ -104,56 +116,78 @@ import static fr.gaulupeau.apps.Poche.Helpers.getInputStreamFromUrl;
btnSync.setOnClickListener(new OnClickListener() { btnSync.setOnClickListener(new OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
// JSONRPCClient client = JSONRPCClient.create("http://v2.inthepoche.com/jsonrpc.php", JSONRPCParams.Versions.VERSION_2); // Vérification de la connectivité Internet
// client.setConnectionTimeout(8000); final ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
// client.setSoTimeout(8000); final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
// JSONObject params = new JSONObject(); if (activeNetwork != null && activeNetwork.isConnected()) {
// try { // Exécution de la synchro en arrière-plan
// params.put("admin", "PYBRAYc4ebUuRoa"); new Thread(new Runnable() {
// String ret = client.callString("authentication"); public void run() {
// Log.e("API", ret); String id = "req-001";
// } catch (Exception e) { JSONRPC2Request reqOut = null;
// // TODO Auto-generated catch block try {
// e.printStackTrace(); // 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]}");
// Vérification de la connectivité Internet // GET A LINK
final ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); reqOut = JSONRPC2Request.parse("{\"jsonrpc\":\"2.0\",\"method\":\"item.info\",\"id\":\"" + id + "\",\"params\":[{\"username\":\""+ apiUsername + "\",\"api_token\":\""+ apiToken +"\"}, 1]}");
final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo(); } catch (JSONRPC2ParseException e2) {
if (activeNetwork != null && activeNetwork.isConnected()) { e2.printStackTrace();
// Exécution de la synchro en arrière-plan }
new Thread(new Runnable() { System.out.println(reqOut.toString());
public void run() { URL url = null;
String ret = getInputStreamFromUrl("http://poche.gaulupeau.fr/toto.php"); try {
ArticlesSQLiteOpenHelper helper = new ArticlesSQLiteOpenHelper(getApplicationContext()); final String rpcuser ="api_user";
database = helper.getWritableDatabase(); final String rpcpassword = globalToken;
try {
JSONArray rootobj = new JSONArray(ret); Authenticator.setDefault(new Authenticator() {
for (int i=0; i<rootobj.length(); i++) { protected PasswordAuthentication getPasswordAuthentication() {
JSONObject article = rootobj.getJSONObject(i); return new PasswordAuthentication (rpcuser, rpcpassword.toCharArray());
ContentValues values = new ContentValues(); }});
values.put(ARTICLE_TITLE, Html.fromHtml(article.getString("titre")).toString()); url = new URL(pocheUrl + "/jsonrpc.php");
values.put(ARTICLE_CONTENT, Html.fromHtml(article.getString("content")).toString()); } catch (MalformedURLException e1) {
values.put(ARTICLE_ID, Html.fromHtml(article.getString("id")).toString()); e1.printStackTrace();
values.put(ARTICLE_URL, Html.fromHtml(article.getString("url")).toString()); }
values.put(ARCHIVE, 0); JSONRPC2Session session = new JSONRPC2Session(url);
try { JSONRPC2Response response = null;
database.insertOrThrow(ARTICLE_TABLE, null, values); try{
} catch (SQLiteConstraintException e) { response = session.send(reqOut);
continue; } catch (JSONRPC2SessionException e) {
}
} System.err.println(e.getMessage());
showToast(getString(R.string.txtSyncDone)); }
updateUnread(); if (response.indicatesSuccess()){
} catch (JSONException e) { JSONObject article = null;
e.printStackTrace(); ContentValues values = new ContentValues();
showToast(getString(R.string.txtSyncFailed)); try {
} article = new JSONObject(response.getResult().toString());
} values.put(ARTICLE_TITLE, Html.fromHtml(article.getString("title")).toString());
}).start(); values.put(ARTICLE_CONTENT, Html.fromHtml(article.getString("content")).toString());
} else { values.put(ARTICLE_ID, Html.fromHtml(article.getString("id")).toString());
// Afficher alerte connectivité values.put(ARTICLE_URL, Html.fromHtml(article.getString("url")).toString());
showToast(getString(R.string.txtNetOffline)); 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();
} else {
// Afficher alerte connectivité
showToast(getString(R.string.txtNetOffline));
}
} }
}); });

View File

@ -13,6 +13,9 @@ import android.widget.EditText;
public class Settings extends Activity { public class Settings extends Activity {
Button btnDone; Button btnDone;
EditText editPocheUrl; EditText editPocheUrl;
EditText editAPIUsername;
EditText editAPIToken;
EditText editGlobalToken;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -20,15 +23,26 @@ public class Settings extends Activity {
setContentView(R.layout.settings); setContentView(R.layout.settings);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String pocheUrl = settings.getString("pocheUrl", "http://"); String pocheUrl = settings.getString("pocheUrl", "http://");
String apiUsername = settings.getString("APIUsername", "");
String apiToken = settings.getString("APIToken", "");
String globalToken = settings.getString("globalToken", "");
editPocheUrl = (EditText)findViewById(R.id.pocheUrl); editPocheUrl = (EditText)findViewById(R.id.pocheUrl);
editPocheUrl.setText(pocheUrl); editPocheUrl.setText(pocheUrl);
editAPIUsername = (EditText)findViewById(R.id.APIUsername);
editAPIUsername.setText(apiUsername);
editAPIToken = (EditText)findViewById(R.id.APIToken);
editAPIToken.setText(apiToken);
editGlobalToken = (EditText)findViewById(R.id.globalToken);
editGlobalToken.setText(globalToken);
btnDone = (Button)findViewById(R.id.btnDone); btnDone = (Button)findViewById(R.id.btnDone);
btnDone.setOnClickListener(new OnClickListener() { btnDone.setOnClickListener(new OnClickListener() {
public void onClick(View v) { public void onClick(View v) {
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit(); SharedPreferences.Editor editor = settings.edit();
editor.putString("pocheUrl", editPocheUrl.getText().toString()); editor.putString("pocheUrl", editPocheUrl.getText().toString());
editor.putString("APIUsername", editAPIUsername.getText().toString());
editor.putString("APIToken", editAPIToken.getText().toString());
editor.putString("globalToken", editGlobalToken.getText().toString());
editor.commit(); editor.commit();
finish(); finish();
} }