0
\$\begingroup\$

I wrote a simple android app to export the browsers history as JSON, but I do not know if the resulting JSON is a format that the user will easily be able to parse and use.

Is there any way that i could improve the resulting format? Will it be easy for the user to use?

I have the title of the page first, should I have the URL first instead?

The URL contains escape characters, but this does not work in all browsers, For example Chrome can reformat the url but Safari can't. Is there a way to fix this so the URL will work in all browsers?

Here is the code for the app.

public class MainActivity extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 getBrowserHist();
 }
 public void getBrowserHist() {
 try {
 JSONObject parent = new JSONObject();
 String[] proj = new String[] { Browser.BookmarkColumns.TITLE,
 Browser.BookmarkColumns.URL, Browser.BookmarkColumns.DATE,
 Browser.BookmarkColumns.VISITS };
 String sel = Browser.BookmarkColumns.BOOKMARK + " = 0";
 Cursor mCur = getContentResolver().query(Browser.BOOKMARKS_URI,
 proj, sel, null, null);
 mCur.moveToFirst();
 if (mCur.moveToFirst() && mCur.getCount() > 0) {
 boolean cont = true;
 while (mCur.isAfterLast() == false && cont) {
 JSONObject jsonObject = new JSONObject();
 String title = mCur.getString(mCur
 .getColumnIndex(Browser.BookmarkColumns.TITLE));
 String url = mCur.getString(mCur
 .getColumnIndex(Browser.BookmarkColumns.URL));
 String date = mCur.getString(mCur
 .getColumnIndex(Browser.BookmarkColumns.DATE));
 String visits = mCur.getString(mCur
 .getColumnIndex(Browser.BookmarkColumns.VISITS));
 jsonObject.put("TITLE", title);
 jsonObject.put("URL", url);
 jsonObject.put("DATE", date);
 jsonObject.put("VISITS", visits);
 parent.put(title, jsonObject);
 mCur.moveToNext();
 }
 EditText output = (EditText) findViewById(R.id.outputArea);
 output.setText(parent.toString(4));
 }
 } catch (JSONException e) {
 e.printStackTrace();
 }
 Button copy = (Button) findViewById(R.id.copy);
 copy.setOnClickListener(new OnClickListener() {
 @TargetApi(Build.VERSION_CODES.HONEYCOMB)
 @Override
 public void onClick(View v) {
 EditText output = (EditText) findViewById(R.id.outputArea);
 String data = output.getText().toString();
 ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
 ClipData clip = ClipData.newPlainText("label", data);
 clipboard.setPrimaryClip(clip);
 }
 });
 Button share = (Button) findViewById(R.id.share);
 share.setOnClickListener(new OnClickListener() {
 @Override
 public void onClick(View v) {
 EditText output = (EditText) findViewById(R.id.outputArea);
 String data = output.getText().toString();
 Intent sendIntent = new Intent();
 sendIntent.setAction(Intent.ACTION_SEND);
 sendIntent.putExtra(Intent.EXTRA_TEXT, data);
 sendIntent.setType("text/plain");
 startActivity(sendIntent);
 }
 });
 }
}

Here is a sample of the resulting JSON.

{
 "Academia Stack Exchange": {
 "DATE": "1388375986375",
 "URL": "http:\/\/academia.stackexchange.com\/",
 "VISITS": "1",
 "TITLE": "Academia Stack Exchange"
 },
 "ProgrammableWeb: Subscribe": {
 "DATE": "1388307236378",
 "URL": "http:\/\/www.programmableweb.com\/pwtsubscribe",
 "VISITS": "1",
 "TITLE": "ProgrammableWeb: Subscribe"
 },
 "Bitcoin Block Explorer - Blockchain.info": {
 "DATE": "1388306500160",
 "URL": "http:\/\/blockchain.info\/",
 "VISITS": "1",
 "TITLE": "Bitcoin Block Explorer - Blockchain.info"
 },
 "All Sites - Stack Exchange": {
 "DATE": "1388375962623",
 "URL": "http:\/\/stackexchange.com\/sites",
 "VISITS": "1",
 "TITLE": "All Sites - Stack Exchange"
 },
 "Block Explorer API - blockchain.info": {
 "DATE": "1388306537030",
 "URL": "http:\/\/blockchain.info\/api\/blockchain_api",
 "VISITS": "3",
 "TITLE": "Block Explorer API - blockchain.info"
 },
 "ProgrammableWeb: Subscribe Success": {
 "DATE": "1388307257562",
 "URL": "http:\/\/www.programmableweb.com\/pwtsubscribesuccess",
 "VISITS": "2",
 "TITLE": "ProgrammableWeb: Subscribe Success"
 },
 "http:\/\/blockchain.info\/rawaddr\/1CuJxwjqf8z2uy3EHh2Kge12tcR5eNiB4t": {
 "DATE": "1388306616394",
 "URL": "http:\/\/blockchain.info\/rawaddr\/1CuJxwjqf8z2uy3EHh2Kge12tcR5eNiB4t",
 "VISITS": "1",
 "TITLE": "http:\/\/blockchain.info\/rawaddr\/1CuJxwjqf8z2uy3EHh2Kge12tcR5eNiB4t"
 },
 "Code Review Stack Exchange": {
 "DATE": "1388376002575",
 "URL": "http:\/\/codereview.stackexchange.com\/",
 "VISITS": "1",
 "TITLE": "Code Review Stack Exchange"
 },
 "Stack Overflow": {
 "DATE": "1388375977992",
 "URL": "http:\/\/stackoverflow.com\/?as=1",
 "VISITS": "1",
 "TITLE": "Stack Overflow"
 }
}

Here is the repository for the app.

asked Dec 30, 2013 at 4:12
\$\endgroup\$
1
  • \$\begingroup\$ I'm not sure this question is in-topic. We do code reviews, not suggestions on how to make an output format better. \$\endgroup\$ Commented Dec 30, 2013 at 12:33

1 Answer 1

1
\$\begingroup\$

If you want to use the JSON to be easily parsed, I would suggest using a JSONArray so your output is ordered the way it is in the browsers history (unlike the JSONObject, which are key/value pairs not in order). If you use JSONArray you should save the title inside the objects of course.


If you want to have it human readable, how about exporting it as .pdf or .html file with clickable links.


If you want to do some more work, you could use the jsondata, export it as your own data-format, and build an Activity for reading and displaying this data to make it human readable.

You could easily un-escape the URLs, then they should work in every browser.


Small code correction: your variable cont in line 37 should be obsolete.

Malachi
29k11 gold badges86 silver badges188 bronze badges
answered Dec 30, 2013 at 12:40
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.