android - 如何使用 POST 为 Parse 服务器上传图像?

标签 android image http post upload

我必须使用 HTTP Post 请求上传我的图片。图片将发布到 Parse server .

我找到了这个文档,但我不确定如何创建上传图像。我有一个http请求方法,请指正。

这里是 the documentation .

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 ;
    }

最佳答案

这是我目前使用的

        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;
    }

关于android - 如何使用 POST 为 Parse 服务器上传图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10010085/

相关文章:

c++ - 用 QTextEdit 中的另一个图像替换图像

html - 投资组合显示相同的图片而不是不同的图片 - html、css

ios - 我怎样才能实现这个图像效果 iOS (看图片)

http - OpenStack Swift 无法使用批量操作自动提取 tar 文件

Android:.nomedia 被忽略,图像仍然出现在图库中

java - 如何知道日期是否在前 x 分钟内?

android - 尽管匹配,PackageManager queryIntentActivities 返回空列表

java - Apache HTTP : How to set custom ContentType

node.js - 如何从本地主机向我的 docker 容器发送 HTTP 请求?

android 获取毫秒级的日期