List of usage examples for android.net Uri.Builder toString
public abstract String toString();
From source file:com.google.samples.apps.iosched.nearby.MetadataResolver.java
private static JsonObjectRequest createMetadataRequest(JSONObject jsonObj, final Map<String, NearbyDevice> deviceMap) { return new JsonObjectRequest(METADATA_URL, jsonObj, new Response.Listener<JSONObject>() { @Override// w w w .ja va 2 s . co m public void onResponse(JSONObject jsonResponse) { try { JSONArray foundMetaData = jsonResponse.getJSONArray("metadata"); int deviceCount = foundMetaData.length(); for (int i = 0; i < deviceCount; i++) { JSONObject deviceData = foundMetaData.getJSONObject(i); String title = "Unknown name"; String url = "Unknown url"; String description = "Unknown description"; String iconUrl = "/favicon.ico"; String id = deviceData.getString("id"); if (deviceData.has("title")) { title = deviceData.getString("title"); } if (deviceData.has("url")) { url = deviceData.getString("url"); } if (deviceData.has("description")) { description = deviceData.getString("description"); } if (deviceData.has("icon")) { // We might need to do some magic here. iconUrl = deviceData.getString("icon"); } // Provisions for a favicon specified as a relative URL. if (!iconUrl.startsWith("http")) { // Lets just assume we are dealing with a relative path. Uri fullUri = Uri.parse(url); Uri.Builder builder = fullUri.buildUpon(); // Append the default favicon path to the URL. builder.path(iconUrl); iconUrl = builder.toString(); } DeviceMetadata deviceMetadata = new DeviceMetadata(); deviceMetadata.title = title; deviceMetadata.description = description; deviceMetadata.siteUrl = url; deviceMetadata.iconUrl = iconUrl; downloadIcon(deviceMetadata, deviceMap.get(id)); // Look up the device from the input and update the data deviceMap.get(id).onDeviceInfo(deviceMetadata); } } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { Log.i(TAG, "VolleyError: " + volleyError.toString()); } }); }
From source file:org.LinuxdistroCommunity.android.client.NetworkUtilities.java
private static URL getRequestURL(String server, String uri, ArrayList<NameValuePair> params) throws MalformedURLException { Uri server_uri = Uri.parse(server + uri); Uri.Builder request_builder = server_uri.buildUpon(); for (NameValuePair item : params) { request_builder.appendQueryParameter(item.getName(), item.getValue()); }// www.j a v a2 s.co m request_builder.appendQueryParameter(PARAM_CLIENT_ID, CLIENT_ID); return new URL(request_builder.toString()); }
From source file:physical_web.org.physicalweb.MetadataResolver.java
/** * Create the url metadata request,//w ww . j av a2 s.c o m * given the json request object * * @param jsonObj * @return */ private static JsonObjectRequest createUrlMetadataRequest(JSONObject jsonObj) { return new JsonObjectRequest(METADATA_URL, jsonObj, new Response.Listener<JSONObject>() { // Called when the server returns a response @Override public void onResponse(JSONObject jsonResponse) { // Build the metadata from the response try { JSONArray foundMetaData = jsonResponse.getJSONArray("metadata"); if (foundMetaData.length() > 0) { JSONObject jsonUrlMetadata = foundMetaData.getJSONObject(0); String title = "Unknown name"; String url = "Unknown url"; String description = "Unknown description"; String iconUrl = "/favicon.ico"; String id = jsonUrlMetadata.getString("id"); if (jsonUrlMetadata.has("title")) { title = jsonUrlMetadata.getString("title"); } if (jsonUrlMetadata.has("url")) { url = jsonUrlMetadata.getString("url"); } if (jsonUrlMetadata.has("description")) { description = jsonUrlMetadata.getString("description"); } if (jsonUrlMetadata.has("icon")) { // We might need to do some magic here. iconUrl = jsonUrlMetadata.getString("icon"); } // TODO: Eliminate this fallback since we expect the server to always return an icon. // Provisions for a favicon specified as a relative URL. if (!iconUrl.startsWith("http")) { // Lets just assume we are dealing with a relative path. Uri fullUri = Uri.parse(url); Uri.Builder builder = fullUri.buildUpon(); // Append the default favicon path to the URL. builder.path(iconUrl); iconUrl = builder.toString(); } UrlMetadata urlMetadata = new UrlMetadata(); urlMetadata.title = title; urlMetadata.description = description; urlMetadata.siteUrl = url; urlMetadata.iconUrl = iconUrl; downloadIcon(urlMetadata, url); onUrlMetadataReceived(id, urlMetadata); } } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { Log.i(TAG, "VolleyError: " + volleyError.toString()); } }); }
From source file:com.carltondennis.glassphysicalweb.MetadataResolver.java
/** * Create the url metadata request,//w ww . j ava 2s. com * given the json request object * * @param jsonObj The given json object to use in the request * @return The created json request object */ private static JsonObjectRequest createUrlMetadataRequest(JSONObject jsonObj, final boolean isDemoRequest) { return new JsonObjectRequest(isDemoRequest ? DEMO_METADATA_URL : METADATA_URL, jsonObj, new Response.Listener<JSONObject>() { // Called when the server returns a response @Override public void onResponse(JSONObject jsonResponse) { // Build the metadata from the response try { JSONArray foundMetaData = jsonResponse.getJSONArray("metadata"); // Loop through the metadata for each url if (foundMetaData.length() > 0) { for (int i = 0; i < foundMetaData.length(); i++) { JSONObject jsonUrlMetadata = foundMetaData.getJSONObject(i); String title = ""; String url = ""; String description = ""; String iconUrl = "/favicon.ico"; String id = jsonUrlMetadata.getString("id"); if (jsonUrlMetadata.has("title")) { title = jsonUrlMetadata.getString("title"); } if (jsonUrlMetadata.has("url")) { url = jsonUrlMetadata.getString("url"); } if (jsonUrlMetadata.has("description")) { description = jsonUrlMetadata.getString("description"); } if (jsonUrlMetadata.has("icon")) { // We might need to do some magic here. iconUrl = jsonUrlMetadata.getString("icon"); } // TODO: Eliminate this fallback since we expect the server to always return an icon. // Provisions for a favicon specified as a relative URL. if (!iconUrl.startsWith("http")) { // Lets just assume we are dealing with a relative path. Uri fullUri = Uri.parse(url); Uri.Builder builder = fullUri.buildUpon(); // Append the default favicon path to the URL. builder.path(iconUrl); iconUrl = builder.toString(); } // Create the metadata object UrlMetadata urlMetadata = new UrlMetadata(); urlMetadata.title = title; urlMetadata.description = description; urlMetadata.siteUrl = url; urlMetadata.iconUrl = iconUrl; // Kick off the icon download downloadIcon(urlMetadata); if (isDemoRequest) { mMetadataResolverCallback.onDemoUrlMetadataReceived(id, urlMetadata); } else { mMetadataResolverCallback.onUrlMetadataReceived(id, urlMetadata); } } } } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { Log.i(TAG, "VolleyError: " + volleyError.toString()); } }); }
From source file:org.physical_web.physicalweb.MetadataResolver.java
/** * Create the url metadata request,//from w w w. j av a 2 s.c o m * given the json request object * * @param jsonObj The given json object to use in the request * @return The created json request object */ private static JsonObjectRequest createUrlMetadataRequest(JSONObject jsonObj, final boolean isDemoRequest) { return new JsonObjectRequest(isDemoRequest ? DEMO_METADATA_URL : METADATA_URL, jsonObj, new Response.Listener<JSONObject>() { // Called when the server returns a response @Override public void onResponse(JSONObject jsonResponse) { // Build the metadata from the response try { JSONArray foundMetaData = jsonResponse.getJSONArray("metadata"); // Loop through the metadata for each url if (foundMetaData.length() > 0) { for (int i = 0; i < foundMetaData.length(); i++) { JSONObject jsonUrlMetadata = foundMetaData.getJSONObject(i); String title = ""; String url = ""; String description = ""; String iconUrl = "/favicon.ico"; String id = jsonUrlMetadata.getString("id"); float score = UNDEFINED_SCORE; if (jsonUrlMetadata.has("title")) { title = jsonUrlMetadata.getString("title"); } if (jsonUrlMetadata.has("url")) { url = jsonUrlMetadata.getString("url"); } if (jsonUrlMetadata.has("description")) { description = jsonUrlMetadata.getString("description"); } if (jsonUrlMetadata.has("icon")) { // We might need to do some magic here. iconUrl = jsonUrlMetadata.getString("icon"); } if (jsonUrlMetadata.has("score")) { score = Float.parseFloat(jsonUrlMetadata.getString("score")); } // TODO: Eliminate this fallback since we expect the server to always return an icon. // Provisions for a favicon specified as a relative URL. if (!iconUrl.startsWith("http")) { // Lets just assume we are dealing with a relative path. Uri fullUri = Uri.parse(url); Uri.Builder builder = fullUri.buildUpon(); // Append the default favicon path to the URL. builder.path(iconUrl); iconUrl = builder.toString(); } // Create the metadata object UrlMetadata urlMetadata = new UrlMetadata(); urlMetadata.title = title; urlMetadata.description = description; urlMetadata.siteUrl = url; urlMetadata.iconUrl = iconUrl; urlMetadata.score = score; // Kick off the icon download downloadIcon(urlMetadata); if (isDemoRequest) { mMetadataResolverCallback.onDemoUrlMetadataReceived(id, urlMetadata); } else { mMetadataResolverCallback.onUrlMetadataReceived(id, urlMetadata); } } } } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { Log.i(TAG, "VolleyError: " + volleyError.toString()); } }); }
From source file:com.nbplus.vbroadlauncher.service.GetRadioChannelTask.java
@Override protected BaseApiResult doInBackground(Void... voids) { RequestQueue requestQueue = Volley.newRequestQueue(mContext, new HurlStack() { @Override//w w w . j a v a 2 s . c o m protected HttpURLConnection createConnection(URL url) throws IOException { HttpURLConnection connection = super.createConnection(url); // Fix for bug in Android runtime(!!!): // https://code.google.com/p/android/issues/detail?id=24672 connection.setRequestProperty("Accept-Encoding", ""); return connection; } }); RadioChannelInfo response = null; Uri.Builder builder = Uri.parse(mServerPath).buildUpon(); builder.appendQueryParameter("DEVICE_ID", LauncherSettings.getInstance(mContext).getDeviceID()); String url = builder.toString(); // int retryCount = 0; // while (retryCount < 3) { // retry 3 times RequestFuture<RadioChannelInfo> future = RequestFuture.newFuture(); GsonRequest request = new GsonRequest(Request.Method.GET, url, null, RadioChannelInfo.class, future, future); request.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 3, 1.0f)); requestQueue.add(request); try { response = future.get(); // this will block (forever) // break; } catch (InterruptedException e) { // exception handling e.printStackTrace(); } catch (ExecutionException e) { // exception handling e.printStackTrace(); } // retryCount++; // } return (BaseApiResult) response; }
From source file:org.LinuxdistroCommunity.android.client.ApiTest.java
@Override protected JSONObject doInBackground(Void... params) { InputStream is;//from w ww .j av a2s. c om Uri.Builder uri_builder = Uri.parse(mServer + API_BASE + API_TEST).buildUpon(); uri_builder.appendQueryParameter(PARAM_ACCESS_TOKEN, mToken); HttpURLConnection conn; try { conn = (HttpURLConnection) new URL(uri_builder.toString()).openConnection(); conn.setReadTimeout(10000 /* milliseconds */); conn.setConnectTimeout(15000 /* milliseconds */); conn.setRequestMethod("GET"); conn.setDoInput(true); // Starts the query conn.connect(); int response_code = conn.getResponseCode(); Log.d(TAG, "The response is: " + response_code); is = conn.getInputStream(); // parse token_response as JSON to get the token out String str_response = NetworkUtilities.readStreamToString(is, 500); Log.d("GMG", str_response); JSONObject response = new JSONObject(str_response); return response; } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
From source file:com.nbplus.vbroadlistener.gcm.RegistrationIntentService.java
/** * Persist registration to third-party servers. * * Modify this method to associate the user's GCM registration token with any server-side account * maintained by your application./* www . j a va2 s . co m*/ * * @param token The new token. */ private boolean sendRegistrationToServer(String token) { boolean result = false; RequestQueue requestQueue = Volley.newRequestQueue(this, new HurlStack() { @Override protected HttpURLConnection createConnection(URL url) throws IOException { HttpURLConnection connection = super.createConnection(url); // Fix for bug in Android runtime(!!!): // https://code.google.com/p/android/issues/detail?id=24672 connection.setRequestProperty("Accept-Encoding", ""); return connection; } }); BaseApiResult response = null; VBroadcastServer serverInfo = LauncherSettings.getInstance(this).getServerInformation(); if (serverInfo == null || StringUtils.isEmptyString(serverInfo.getApiServer())) { Log.d(TAG, ">> I can't find api server information..."); return result; } String url = serverInfo.getApiServer() + Constants.API_GCM_TOKEN_UPDATE_CONTEXT_PATH; Uri.Builder builder = Uri.parse(url).buildUpon(); url = builder.toString(); if (StringUtils.isEmptyString(LauncherSettings.getInstance(this).getDeviceID())) { String deviceID = LauncherSettings.getInstance(this).getDeviceID(); LauncherSettings.getInstance(this).setDeviceID(deviceID); } String strRequestBody = String.format( "{\"DEVICE_ID\" : \"%s\", \"PUSH_TOKEN\" : \"%s\", \"APP_ID\" : \"%s\"}", LauncherSettings.getInstance(this).getDeviceID(), token, getApplicationContext().getPackageName()); RequestFuture<BaseApiResult> future = RequestFuture.newFuture(); GsonRequest request = new GsonRequest(Request.Method.POST, url, strRequestBody, BaseApiResult.class, future, future); request.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 3, 1.0f)); requestQueue.add(request); try { response = future.get(); // this will block (forever) if (Constants.API_RESP_OK.equals(response.getResultCode())) { result = true; } Log.d(TAG, "GCM token update send rt =" + response.getResultCode()); } catch (InterruptedException e) { // exception handling e.printStackTrace(); } catch (ExecutionException e) { // exception handling e.printStackTrace(); } return result; }
From source file:br.com.vrbsm.fillup.controller.FillUpController.java
public void getFillUp() { Uri.Builder url = Uri.parse(FillUpConstants.BASE_URL + "FillUp").buildUpon(); String className = "Vehicle"; String __type = "Pointer"; String objectId = "LvyRdtxUDl"; url.appendQueryParameter("where", "{\"vehicle\":{\"objectId\":\"" + objectId + "\",\"__type\":\"" + __type + "\",\"className\":\"" + className + "\" }}"); doWebRequest(0, url.toString()); }
From source file:org.quantumbadger.redreader.reddit.api.RedditAPIIndividualSubredditListRequester.java
private void doSubredditListRequest(final RedditSubredditManager.SubredditListType type, final RequestResponseHandler<WritableHashSet, SubredditRequestFailure> handler, final String after) { URI uri;/* w ww . j ava 2 s. c o m*/ switch (type) { case SUBSCRIBED: uri = Constants.Reddit.getUri(Constants.Reddit.PATH_SUBREDDITS_MINE_SUBSCRIBER); break; case MODERATED: uri = Constants.Reddit.getUri(Constants.Reddit.PATH_SUBREDDITS_MINE_MODERATOR); break; case MOST_POPULAR: uri = Constants.Reddit.getUri(Constants.Reddit.PATH_SUBREDDITS_POPULAR); break; default: throw new UnexpectedInternalStateException(type.name()); } if (after != null) { // TODO move this logic to General? final Uri.Builder builder = Uri.parse(uri.toString()).buildUpon(); builder.appendQueryParameter("after", after); uri = General.uriFromString(builder.toString()); } final CacheRequest aboutSubredditCacheRequest = new CacheRequest(uri, user, null, Constants.Priority.API_SUBREDDIT_INVIDIVUAL, 0, CacheRequest.DownloadType.FORCE, Constants.FileType.SUBREDDIT_LIST, true, true, false, context) { @Override protected void onCallbackException(Throwable t) { handler.onRequestFailed( new SubredditRequestFailure(RequestFailureType.PARSE, t, null, "Internal error", url)); } @Override protected void onDownloadNecessary() { } @Override protected void onDownloadStarted() { } @Override protected void onProgress(final boolean authorizationInProgress, long bytesRead, long totalBytes) { } @Override protected void onFailure(RequestFailureType type, Throwable t, StatusLine status, String readableMessage) { handler.onRequestFailed( new SubredditRequestFailure(type, t, status, readableMessage, url.toString())); } @Override protected void onSuccess(CacheManager.ReadableCacheFile cacheFile, long timestamp, UUID session, boolean fromCache, String mimetype) { } @Override public void onJsonParseStarted(JsonValue result, long timestamp, UUID session, boolean fromCache) { try { final HashSet<String> output = new HashSet<String>(); final ArrayList<RedditSubreddit> toWrite = new ArrayList<RedditSubreddit>(); final JsonBufferedObject redditListing = result.asObject().getObject("data"); final JsonBufferedArray subreddits = redditListing.getArray("children"); final JsonBuffered.Status joinStatus = subreddits.join(); if (joinStatus == JsonBuffered.Status.FAILED) { handler.onRequestFailed(new SubredditRequestFailure(RequestFailureType.PARSE, null, null, "Unknown parse error", url.toString())); return; } if (type == RedditSubredditManager.SubredditListType.SUBSCRIBED && subreddits.getCurrentItemCount() == 0 && after == null) { doSubredditListRequest(RedditSubredditManager.SubredditListType.DEFAULTS, handler, null); return; } for (final JsonValue v : subreddits) { final RedditThing thing = v.asObject(RedditThing.class); final RedditSubreddit subreddit = thing.asSubreddit(); subreddit.downloadTime = timestamp; toWrite.add(subreddit); output.add(subreddit.getCanonicalName()); } RedditSubredditManager.getInstance(context, user).offerRawSubredditData(toWrite, timestamp); final String receivedAfter = redditListing.getString("after"); if (receivedAfter != null && type != RedditSubredditManager.SubredditListType.MOST_POPULAR) { doSubredditListRequest(type, new RequestResponseHandler<WritableHashSet, SubredditRequestFailure>() { public void onRequestFailed(SubredditRequestFailure failureReason) { handler.onRequestFailed(failureReason); } public void onRequestSuccess(WritableHashSet result, long timeCached) { output.addAll(result.toHashset()); handler.onRequestSuccess( new WritableHashSet(output, timeCached, type.name()), timeCached); if (after == null) { Log.i("SubredditListRequester", "Got " + output.size() + " subreddits in multiple requests"); } } }, receivedAfter); } else { handler.onRequestSuccess(new WritableHashSet(output, timestamp, type.name()), timestamp); if (after == null) { Log.i("SubredditListRequester", "Got " + output.size() + " subreddits in 1 request"); } } } catch (Exception e) { handler.onRequestFailed(new SubredditRequestFailure(RequestFailureType.PARSE, e, null, "Parse error", url.toString())); } } }; CacheManager.getInstance(context).makeRequest(aboutSubredditCacheRequest); }