Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- android SDK Version using file.saveInBackground
- public void tstPost(String path){
- ParseApplication.getInstance(this).setEntityFile(
- path,"picf1", "media1", null);
- }
- public ParseObject setEntityFile(
- String path, String fileKey, String parentKey, ParseObject parent){
- if(parent == null) parent = setEntity("testing parseDotCom");
- ParseFile pfile = null;
- File f = new File("/mnt/sdcard/DCIM/Camera/",path);
- FileInputStream fin = null;
- FileChannel ch = null;
- try {
- fin = new FileInputStream(f);
- ch = fin.getChannel();
- int size = (int) ch.size();
- MappedByteBuffer buf = ch.map(MapMode.READ_ONLY, 0, size);
- byte[] bytes = new byte[size];
- buf.get(bytes);
- pfile = new ParseFile(fileKey, bytes);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } finally {
- try {
- if (fin != null) {
- fin.close();
- }
- if (ch != null) {
- ch.close();
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- if (pfile !=null){
- pfile.saveInBackground(new SaveCallback(){
- @Override
- public void done(ParseException ex) {
- // private final ParseFile _file = pfile;
- Log.d("ParseApplication", " FILE saveInBack completed");
- // pfile.getUrl();
- }
- });
- parent.put(parentKey, pfile);
- }
- return parent;
- }
- * * * Android result upload 63 jpg files avg sz 80K ** 8 Minutes to upload **
- 10-17 08:22:36.447 D/ParseView(21707): STARTED * * * * *
- 10-17 08:30:50.604 D/ParseApplication(21707): FILE saveInBack completed
- Native REST API on android using HttpClient libs and Jackson libs and multithreaded ConnPOOL
- public void tstPost(String path){
- Handler handler = new Handler() {
- public void handleMessage(Message message) {
- switch (message.what) {
- case HttpConnection.DID_START: {
- break;
- }
- case HttpConnection.DID_SUCCEED: {
- byte[] response = (byte[]) message.obj;
- try {
- node = new ObjectMapper().readValue(new ByteArrayInputStream(response), JsonNode.class);
- picFileOb = new ObjectMapper().createObjectNode();
- picFileOb.put("__type", "File");
- picFileOb.put("name", node.path("name").getTextValue());
- picFileOb.put("url", node.path("url").getTextValue());
- Log.d("ParseView", "POST_PIC" +node.path("url").getTextValue()) ;
- } catch (JsonParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (JsonMappingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- break;
- }
- case HttpConnection.DID_ERROR: {
- Exception e = (Exception) message.obj;
- e.printStackTrace();
- Log.d("ParseView", "Connection failed.");
- break;
- }
- }
- }
- };
- new HttpConnection(handler)
- .post(ParseApplication.getInstance().PARSE_FILE_PIC, path);
- }
- *** .post(url, file) method runnable below:
- public void run() {
- handler.sendMessage(Message.obtain(handler, HttpConnection.DID_START));
- HttpParams params = new BasicHttpParams();
- HttpConnectionParams.setConnectionTimeout(params, 40 * 1000);
- HttpConnectionParams.setSoTimeout(params, 10 * 1000);
- HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
- httpClient = new DefaultHttpClient(MyConnectionManager.getInstance(), params);
- // HttpConnectionParams.setSoTimeout(httpClient.getParams(), 25000);
- httpClient.addRequestInterceptor(new HttpRequestInterceptor() {
- public void process(
- final HttpRequest request,
- final HttpContext context) throws HttpException, IOException {
- request.addHeader("X-Parse-Application-Id", ParseApplication.getInstance().key_appId);
- request.addHeader("X-Parse-REST-API-Key", ParseApplication.getInstance().key_rest);
- if (request.getRequestLine().getMethod() == "POST"){
- request.addHeader("Content-Type", httpctyp);
- }
- }
- });
- try {
- HttpResponse response = null;
- switch (method) {
- case GET:
- HttpGet httpGet = new HttpGet(url);
- response = httpClient.execute(httpGet);
- break;
- case POST:
- HttpPost httpPost = new HttpPost(url);
- httpctyp = url.endsWith("pic") ?"image/jpeg" :"audio/*" ;
- FileInputStream fis = new FileInputStream(new File(data));
- FileChannel fc = fis.getChannel();
- int sz = (int)fc.size();
- MappedByteBuffer bb = fc.map(
- FileChannel.MapMode.READ_ONLY, 0, sz);
- data2 = new byte[bb.remaining()];
- bb.get(data2);
- ByteArrayEntity reqEntity = new ByteArrayEntity(data2);
- httpPost.setEntity(reqEntity);
- response = httpClient.execute(httpPost);
- break;
- case PUT:
- case DELETE:
- case BITMAP:
- }
- if (method < BITMAP)
- processEntity(response.getEntity());
- } catch (Exception e) {
- handler.sendMessage(Message.obtain(handler,
- HttpConnection.DID_ERROR, e));
- }
- MyConnectionManager.getInstance().didComplete(this);
- }
- * * * REST API upload 63 jpg files avg sz 80K ** 3 Seconds to upload **
- 10-17 07:58:51.533 D/ParseView(21029): STARTED * * * * *
- 10-17 07:58:54.197 D/ParseView(21029): POST_PIChttp://files.parse.com/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement