Advertisement
Guest User

Parse(dot)com REST API vs Android SDK

a guest
Oct 17th, 2012
1,220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.01 KB | None | 0 0
  1. android SDK Version using file.saveInBackground
  2.  
  3.        public void tstPost(String path){
  4.            ParseApplication.getInstance(this).setEntityFile(
  5.                    path,"picf1", "media1", null);
  6.        }
  7.  
  8.     public ParseObject setEntityFile(
  9.             String path, String fileKey, String parentKey, ParseObject parent){
  10.         if(parent == null) parent = setEntity("testing parseDotCom");
  11.         ParseFile pfile = null;
  12.         File f = new File("/mnt/sdcard/DCIM/Camera/",path);
  13.             FileInputStream fin = null;
  14.             FileChannel ch = null;
  15.             try {
  16.                 fin = new FileInputStream(f);
  17.                 ch = fin.getChannel();
  18.                 int size = (int) ch.size();
  19.                 MappedByteBuffer buf = ch.map(MapMode.READ_ONLY, 0, size);
  20.                 byte[] bytes = new byte[size];
  21.                 buf.get(bytes);
  22.                 pfile = new ParseFile(fileKey, bytes);
  23.             } catch (IOException e) {
  24.                 // TODO Auto-generated catch block
  25.                 e.printStackTrace();
  26.             } finally {
  27.                 try {
  28.                     if (fin != null) {
  29.                         fin.close();
  30.                     }
  31.                     if (ch != null) {
  32.                         ch.close();
  33.                     }
  34.                 } catch (IOException e) {
  35.                     // TODO Auto-generated catch block
  36.                     e.printStackTrace();
  37.                 }              
  38.             }
  39.             if (pfile !=null){
  40.                 pfile.saveInBackground(new SaveCallback(){
  41.                     @Override
  42.                     public void done(ParseException ex) {
  43.                      //   private final ParseFile _file = pfile;
  44.                         Log.d("ParseApplication", " FILE saveInBack completed");
  45.                      //   pfile.getUrl();                    
  46.                     }
  47.                   });
  48.              parent.put(parentKey, pfile);
  49.             }
  50.             return parent;
  51.     }
  52.  
  53. * * * Android result upload 63 jpg files avg sz 80K ** 8 Minutes to upload  **
  54. 10-17 08:22:36.447 D/ParseView(21707): STARTED * * * * *
  55. 10-17 08:30:50.604 D/ParseApplication(21707):  FILE saveInBack completed
  56.  
  57. Native REST API on android using HttpClient libs and Jackson libs and multithreaded ConnPOOL
  58.  
  59.        public void tstPost(String path){
  60.            Handler handler = new Handler() {
  61.                public void handleMessage(Message message) {
  62.                  switch (message.what) {
  63.                  case HttpConnection.DID_START: {
  64.                    break;
  65.                  }
  66.                  case HttpConnection.DID_SUCCEED: {
  67.                     byte[] response = (byte[]) message.obj;
  68.                     try {                                              
  69.                         node = new ObjectMapper().readValue(new ByteArrayInputStream(response), JsonNode.class);   
  70.                         picFileOb = new ObjectMapper().createObjectNode();
  71.                         picFileOb.put("__type", "File");
  72.                         picFileOb.put("name", node.path("name").getTextValue());
  73.                         picFileOb.put("url", node.path("url").getTextValue());                                                                                             
  74.                         Log.d("ParseView", "POST_PIC" +node.path("url").getTextValue()) ;              
  75.                     } catch (JsonParseException e) {
  76.                         // TODO Auto-generated catch block
  77.                         e.printStackTrace();
  78.                     } catch (JsonMappingException e) {
  79.                         // TODO Auto-generated catch block
  80.                         e.printStackTrace();
  81.                     } catch (IOException e) {
  82.                         // TODO Auto-generated catch block
  83.                         e.printStackTrace();
  84.                     }                      
  85.                    break;
  86.                  }
  87.                  case HttpConnection.DID_ERROR: {                  
  88.                    Exception e = (Exception) message.obj;
  89.                    e.printStackTrace();
  90.                    Log.d("ParseView", "Connection failed.");
  91.                    break;
  92.                  }
  93.                }
  94.              }
  95.            };        
  96.            new HttpConnection(handler)
  97.              .post(ParseApplication.getInstance().PARSE_FILE_PIC, path);
  98.        }
  99.  
  100. *** .post(url, file) method runnable below:
  101.  
  102.     public void run() {
  103.         handler.sendMessage(Message.obtain(handler, HttpConnection.DID_START));
  104.         HttpParams params = new BasicHttpParams();
  105.         HttpConnectionParams.setConnectionTimeout(params, 40 * 1000);
  106.         HttpConnectionParams.setSoTimeout(params, 10 * 1000);
  107.         HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
  108.        
  109.         httpClient = new DefaultHttpClient(MyConnectionManager.getInstance(), params);
  110. //      HttpConnectionParams.setSoTimeout(httpClient.getParams(), 25000);
  111.         httpClient.addRequestInterceptor(new HttpRequestInterceptor() {
  112.             public void process(
  113.                 final HttpRequest request,
  114.                 final HttpContext context) throws HttpException, IOException {                
  115.                 request.addHeader("X-Parse-Application-Id", ParseApplication.getInstance().key_appId);
  116.                 request.addHeader("X-Parse-REST-API-Key", ParseApplication.getInstance().key_rest);                
  117.                 if (request.getRequestLine().getMethod() == "POST"){                   
  118.                      request.addHeader("Content-Type", httpctyp);
  119.                 }
  120.             }          
  121.         });
  122.         try {                      
  123.             HttpResponse response = null;
  124.             switch (method) {
  125.             case GET:
  126.                 HttpGet httpGet = new HttpGet(url);            
  127.                 response = httpClient.execute(httpGet);
  128.                 break;
  129.             case POST:
  130.                 HttpPost httpPost = new HttpPost(url);
  131.                 httpctyp = url.endsWith("pic") ?"image/jpeg" :"audio/*" ;                      
  132.                 FileInputStream fis = new FileInputStream(new File(data));
  133.                 FileChannel fc = fis.getChannel();
  134.                             int sz = (int)fc.size();                       
  135.                 MappedByteBuffer bb = fc.map(
  136.                 FileChannel.MapMode.READ_ONLY, 0, sz);
  137.                 data2 = new byte[bb.remaining()];
  138.                 bb.get(data2);
  139.                 ByteArrayEntity reqEntity = new ByteArrayEntity(data2);            
  140.                 httpPost.setEntity(reqEntity);
  141.                 response = httpClient.execute(httpPost);
  142.                 break;
  143.             case PUT:
  144.             case DELETE:
  145.             case BITMAP:
  146.             }
  147.             if (method < BITMAP)
  148.                 processEntity(response.getEntity());
  149.         } catch (Exception e) {
  150.             handler.sendMessage(Message.obtain(handler,
  151.                     HttpConnection.DID_ERROR, e));
  152.         }
  153.         MyConnectionManager.getInstance().didComplete(this);
  154.     }
  155.  
  156. * * * REST API upload 63 jpg files avg sz 80K ** 3 Seconds to upload  **
  157. 10-17 07:58:51.533 D/ParseView(21029): STARTED * * * * *
  158. 10-17 07:58:54.197 D/ParseView(21029): POST_PIChttp://files.parse.com/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement