2012-04-04 14 views
2

HTTPポストリクエストを使用して画像をアップロードする必要があります。画像はparse serverに送信されますパースサーバー用のPOSTを使用して画像をアップロードする方法

この文書は見つかりましたが、画像をアップロードする方法がわかりません。私を助けてください。私はHTTPリクエストメソッドを持っています、私を修正してください。

ここにはドキュメントがあります。 https://www.parse.com/docs/rest#files

public boolean uploadingFiles (String tempFilePath) { 
     String filePath = null ; 
     String fileName = null ; 
     boolean flag = false ; 
     try { 
      filePath = tempFilePath ; 
      fileName = new String (filePath) ; 
      fileName = filePath.substring (filePath.lastIndexOf ("/") + 1 , filePath.length ()) ; 
     } catch (Exception e) { 
      Log.e ("Exception" , "uploadingFiles Message = " + e.toString ()) ; 
     } 

     HttpClient httpclient = new DefaultHttpClient () ; 
     try { 

      HttpPost httppost = new HttpPost ("https://api.parse.com/1/files/") ; 
      StringBody filename = new StringBody (fileName) ; 
      File f = new File (filePath) ; 
      FileBody bin = new FileBody (f) ; 

      MultipartEntity reqEntity = new MultipartEntity () ; 
      reqEntity.addPart ("X-Parse-Application-Id" , new StringBody("MY KEY")) ; 
      reqEntity.addPart ("X-Parse-REST-API-Key" , new StringBody("My KEY")) ; 
      reqEntity.addPart ("Content-Type:" , new StringBody("image/png")) ; 
      reqEntity.addPart ("file:" , bin) ; 

      httppost.setEntity (reqEntity) ; 

      System.out.println ("executing request " + httppost.getRequestLine ()) ; 
      HttpResponse response = httpclient.execute (httppost) ; 
      HttpEntity resEntity = response.getEntity () ; 

      System.out.println ("----------------------------------------") ; 
      System.out.println (response.getStatusLine ()) ; 
      if (resEntity != null) { 
       System.out.println ("Response content length: " + resEntity.getContentLength ()) ; 
       InputStream is = resEntity.getContent () ; 
       if (is != null) { 
        Writer writer = new StringWriter () ; 

        char [ ] buffer = new char [ 1024 ] ; 
        try { 
         Reader reader = new BufferedReader (new InputStreamReader (is , "UTF-8")) ; 
         int n ; 
         while ((n = reader.read (buffer)) != - 1) { 
          writer.write (buffer , 0 , n) ; 
         } 
        } finally { 
         is.close () ; 
        } 
        String serverResult = writer.toString () ; 
        System.out.println ("Response content: " + serverResult) ; 
       } else { 
        System.out.println ("Response nothing: ") ; 
       } 
      } 
      EntityUtils.consume (resEntity) ; 
     } catch (Exception e) { 
      e.printStackTrace () ; 
     } finally { 
      try { 
       httpclient.getConnectionManager ().shutdown () ; 
      } catch (Exception ignore) { 
      } 
     } 
     return flag ; 
    } 
+0

こんにちは@Arslanがあなたの問題が解決しない使用するものです。 – sandy

答えて

2

これは私が現在

 try 
    { 
     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
     bitmap.compress(CompressFormat.JPEG, 100, bos); 
     byte[] data = bos.toByteArray(); 

     HttpParams httpParameters = new BasicHttpParams(); 
     HttpConnectionParams.setConnectionTimeout(httpParameters, timeout); 
     HttpConnectionParams.setSoTimeout(httpParameters, timeout); 
     HttpClient httpClient = new DefaultHttpClient(httpParameters); 

     HttpPost postRequest = new HttpPost(URL_SEND); 

     ByteArrayBody bab = new ByteArrayBody(data, "Feest" + pad(random.nextInt(9999) + 1) + ".jpg"); 
     MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
     reqEntity.addPart("Filedata", bab); 
     reqEntity.addPart("dropboxId", new StringBody(URLEncoder.encode(uid))); 
     postRequest.setEntity(reqEntity); 

     HttpResponse response = httpClient.execute(postRequest); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8")); 
     String sResponse; 
     StringBuilder s = new StringBuilder(); 

     while((sResponse = reader.readLine()) != null) 
     { 
      s = s.append(sResponse); 
     } 

     if(d) Log.i(E, "Send response:\n" + s); 
    } 
    catch (Exception e) 
    { 
     if(d) Log.e(E, "Error while sending: " + e.getMessage()); 
     return ERROR; 
    } 
+0

こんにちは@userあなたはもっと詳細なコードを提供することができます.. – sandy

関連する問題