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,18 +116,6 @@ 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);
// client.setConnectionTimeout(8000);
// client.setSoTimeout(8000);
// JSONObject params = new JSONObject();
// try {
// params.put("admin", "PYBRAYc4ebUuRoa");
// String ret = client.callString("authentication");
// Log.e("API", ret);
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// Vérification de la connectivité Internet // Vérification de la connectivité Internet
final ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); final ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo(); final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
@ -123,29 +123,62 @@ 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 ret = getInputStreamFromUrl("http://poche.gaulupeau.fr/toto.php"); String id = "req-001";
ArticlesSQLiteOpenHelper helper = new ArticlesSQLiteOpenHelper(getApplicationContext()); JSONRPC2Request reqOut = null;
database = helper.getWritableDatabase();
try { try {
JSONArray rootobj = new JSONArray(ret); // POCHE A LINK
for (int i=0; i<rootobj.length(); i++) { //reqOut = JSONRPC2Request.parse("{\"jsonrpc\":\"2.0\",\"method\":\"item.add\",\"id\":\"req-001\",\"params\":[{\"username\":\"poche\",\"api_token\":\"cPG2urVgA+ToMXY\"},\"http://cdetc.fr\",true]}");
JSONObject article = rootobj.getJSONObject(i); // 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(); ContentValues values = new ContentValues();
values.put(ARTICLE_TITLE, Html.fromHtml(article.getString("titre")).toString()); 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_CONTENT, Html.fromHtml(article.getString("content")).toString());
values.put(ARTICLE_ID, Html.fromHtml(article.getString("id")).toString()); values.put(ARTICLE_ID, Html.fromHtml(article.getString("id")).toString());
values.put(ARTICLE_URL, Html.fromHtml(article.getString("url")).toString()); values.put(ARTICLE_URL, Html.fromHtml(article.getString("url")).toString());
values.put(ARCHIVE, 0); values.put(ARCHIVE, 0);
} catch (JSONException e) {
e.printStackTrace();
showToast(getString(R.string.txtSyncFailed));
}
try { try {
database.insertOrThrow(ARTICLE_TABLE, null, values); database.insertOrThrow(ARTICLE_TABLE, null, values);
} catch (SQLiteConstraintException e) { } catch (SQLiteConstraintException e) {
continue; e.printStackTrace();
} showToast(getString(R.string.txtSyncFailed));
} }
showToast(getString(R.string.txtSyncDone)); showToast(getString(R.string.txtSyncDone));
updateUnread(); updateUnread();
} catch (JSONException e) { }else{
e.printStackTrace(); System.out.println(response.getError().getMessage( ));
showToast(getString(R.string.txtSyncFailed)); showToast(getString(R.string.txtSyncFailed));
} }
} }
@ -154,6 +187,7 @@ import static fr.gaulupeau.apps.Poche.Helpers.getInputStreamFromUrl;
// Afficher alerte connectivité // Afficher alerte connectivité
showToast(getString(R.string.txtNetOffline)); 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();
} }