public void submitOnClick(View view) { // called when user clicks submit String getRapLeafRequest = getRapLeafRequest(); try { JSONObject jsonobject = new JSONObject(getRapLeafRequest); // stores the response as a JSON object String resultString = jsonobject.toString(); // converts it to a string resultString = resultString.substring( 1, resultString.length() - 2); // parses the information so it can be viewed easier resultString = resultString.replace('"', ' '); resultString = resultString.replaceAll(",", "\n"); TextView results = (TextView) findViewById(R.id.results); results.setText(resultString); Log.v(DEBUG_TAG, jsonobject.toString()); // } } catch (Exception e) { e.printStackTrace(); } }
@Override protected void onActivityResult( int requestCode, int resultCode, Intent data) { // called when contact picker returns if (resultCode == RESULT_OK) { switch (requestCode) { case CONTACT_PICKER_RESULT: String email = ""; String name = ""; Cursor cursor = null; try { /* Bundle extras = data.getExtras(); //stored extras, but didn't need them for this project Set<String> keys = extras.keySet(); Iterator<String> it = keys.iterator(); while (it.hasNext()){ String key = it.next(); Log.v(DEBUG_TAG, key + "[" + extras.get(key) + "]"); } */ Uri result = data.getData(); // get data returned from the contact picker Log.v(DEBUG_TAG, "retrieved result: " + result.toString()); String id = result.getLastPathSegment(); // get id for the contact Log.v(DEBUG_TAG, "element: " + id.toString()); cursor = getContentResolver() .query( Email.CONTENT_URI, null, Email.CONTACT_ID + "=?", new String[] {id}, null); // checks the different columns the cursor contains /* cursor.moveToFirst(); String columns[] = cursor.getColumnNames(); for (String column : columns) { int index = cursor.getColumnIndex(column); Log.v(DEBUG_TAG, "Column: " + column + " == [" + cursor.getString(index) + "]"); } */ if (cursor.moveToFirst()) { int emailIdx = cursor.getColumnIndex(Email.DATA); // email addresses stored in column Email.DATA int nameIdx = cursor.getColumnIndex("display_name"); email = cursor.getString(emailIdx); Log.v(DEBUG_TAG, "nameIdx: " + StructuredName.DISPLAY_NAME); name = cursor.getString(nameIdx); // gets the email and name Log.v(DEBUG_TAG, "read email address: " + email); } else Log.w(DEBUG_TAG, "No results"); } catch (Exception e) { Log.e(DEBUG_TAG, "Failed to get email data"); Log.e(DEBUG_TAG, e.getMessage()); } finally { if (cursor != null) cursor.close(); EditText nameEntry = (EditText) findViewById(R.id.name_form); // sets name to the name form nameEntry.setText(name); if (name.length() == 0) { Toast.makeText(this, "I didn't detect a name :O", Toast.LENGTH_LONG).show(); } EditText emailEntry = (EditText) findViewById(R.id.email_form); // sets email to the email form emailEntry.setText(email); if (email.length() == 0) { Toast.makeText(this, "I didn't detect an email :O", Toast.LENGTH_LONG).show(); } } break; } } else { Log.w(DEBUG_TAG, "Warning: activity result is invalid"); } }