private String uploadMedia( String mediaType, String mediaFilename, String mediaText, String mediaCaption) { // Log.d("uploadMedia()", "param name: " + params.get(index).getName()); /* String baseUrl = BuildConfig.BASE_URL + "/upload_media.json"; double latLng[] = YellrUtils.getLocation(getApplicationContext()); String lat = String.valueOf(latLng[0]); String lng = String.valueOf(latLng[1]); String languageCode = Locale.getDefault().getLanguage(); String url = baseUrl + "?cuid=" + YellrUtils.getCUID(getApplicationContext()) //cuid + "&language_code=" + languageCode + "&lat=" + lat + "&lng=" + lng; */ String uploadMediaJson = "{}"; String baseUrl = BuildConfig.BASE_URL + "/upload_media.json"; String url = YellrUtils.buildUrl(getApplicationContext(), baseUrl); if (url != null) { List<NameValuePair> params = new ArrayList<NameValuePair>(); // params.add(new BasicNameValuePair("cuid", cuid)); params.add(new BasicNameValuePair("media_type", mediaType)); // params.add(new BasicNameValuePair("media_type", mediaType)); if (!mediaFilename.equals("")) { params.add(new BasicNameValuePair("media_file", mediaFilename)); } if (!mediaText.equals("")) { params.add(new BasicNameValuePair("media_text", mediaText)); } if (!mediaCaption.equals("")) { params.add(new BasicNameValuePair("caption", mediaCaption)); } // // derived from // http://stackoverflow.com/a/2937140 // HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpPost httpPost = new HttpPost(url); MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create(); entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); for (int index = 0; index < params.size(); index++) { if (params.get(index).getName().equalsIgnoreCase("media_file") && !mediaType.equalsIgnoreCase("text")) { // we only need to add a file if our media object type is not text // if (mediaType != "text") { // If the key equals to "media_file", we use FileBody to transfer the data Log.d("uploadMedia()", "adding binary file: " + params.get(index).getValue()); entityBuilder.addPart( params.get(index).getName(), new FileBody(new File(params.get(index).getValue()))); // } } else { Log.d("uploadMedia()", "adding text field: " + params.get(index).getValue()); // Normal string data entityBuilder.addPart( params.get(index).getName(), new StringBody(params.get(index).getValue(), ContentType.TEXT_PLAIN)); } } try { HttpEntity entity = entityBuilder.build(); httpPost.setEntity(entity); HttpResponse response = httpClient.execute(httpPost, localContext); InputStream content = response.getEntity().getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(content)); // StringBuilder builder = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { builder.append(line); } uploadMediaJson = builder.toString(); Log.d("PublishPostIntentService.uploadMedia()", "JSON: " + uploadMediaJson); } catch (Exception e) { // Toast.makeText(this, "upload_media: " + e.toString(), Toast.LENGTH_SHORT).show(); Log.d("PublishPostIntentService.uploadMedia()", "ERROR: " + e.toString()); // e.printStackTrace(); } } return uploadMediaJson; }
private String publishPost( int assignmentId, // String languageCode, // String title, // double lat, // double lng, String mediaId) { // String mediaObjects){ /* String baseUrl = BuildConfig.BASE_URL + "/publish_post.json"; // get the location, but if the user has turned off location services, // it will come back null. If it's null, just dump out. // TODO: pop-up a dialog maybe?? double latLng[] = YellrUtils.getLocation(getApplicationContext()); if (latLng == null ) return null; String lat = String.valueOf(latLng[0]); String lng = String.valueOf(latLng[1]); String languageCode = Locale.getDefault().getLanguage(); String url = baseUrl + "?cuid=" + YellrUtils.getCUID(getApplicationContext()) //cuid + "&language_code=" + languageCode + "&lat=" + lat + "&lng=" + lng; */ String publishPostJson = "{}"; String baseUrl = BuildConfig.BASE_URL + "/publish_post.json"; String url = YellrUtils.buildUrl(getApplicationContext(), baseUrl); if (url != null) { List<NameValuePair> params = new ArrayList<NameValuePair>(); // params.add(new BasicNameValuePair("cuid", cuid)); params.add(new BasicNameValuePair("assignment_id", String.valueOf(assignmentId))); // params.add(new BasicNameValuePair("language_code", languageCode)); // params.add(new BasicNameValuePair("title", title)); // params.add(new BasicNameValuePair("lat", String.valueOf(lat))); // params.add(new BasicNameValuePair("lng", String.valueOf(lng))); params.add(new BasicNameValuePair("media_objects", "[\"" + mediaId + "\"]")); // // derived from // http://stackoverflow.com/a/2937140 // HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpPost httpPost = new HttpPost(url); try { MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create(); entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); for (int index = 0; index < params.size(); index++) { entityBuilder.addPart( params.get(index).getName(), new StringBody(params.get(index).getValue(), ContentType.TEXT_PLAIN)); } HttpEntity entity = entityBuilder.build(); httpPost.setEntity(entity); HttpResponse response = httpClient.execute(httpPost, localContext); final String toastMsg; if (response.getStatusLine().getStatusCode() == 200) { toastMsg = "Post Successful!"; } else { toastMsg = "Problem Submitting Post."; } handler.post( new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(), toastMsg, Toast.LENGTH_SHORT).show(); } }); // InputStream content = response.getEntity().getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(content)); // StringBuilder builder = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { builder.append(line); } publishPostJson = builder.toString(); Log.d("PublishPostIntentService.publishPost()", "JSON: " + publishPostJson); } catch (IOException e) { e.printStackTrace(); } } else { Log.d("PublishPostIntentService.publishPost()", "URL was null!!!"); } return publishPostJson; }