Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
针对post方法的Android RestFul API调用_Android_Rest - Fatal编程技术网

针对post方法的Android RestFul API调用

针对post方法的Android RestFul API调用,android,rest,Android,Rest,我对如何发布我的REST端点URL如下所示的数据有疑问: http://my.domain.com/Upload/{ID}/{IMAGE_CONTENT_AS_BYTE_ARRAY} 我需要将图像内容作为字节数组字符串发送到此端点方法。但是由于字符长度可以超过2000个字符,如果图像太大,我可能无法发送图像,因为所有内容都是URL字符串的一部分。如何将图像\u内容\u的数据作为\u字节\u数组。 此外,我没有任何密钥,所以我可以把它放在namevalue对中。请建议 请尝试以下代码: Mult

我对如何发布我的REST端点URL如下所示的数据有疑问:

http://my.domain.com/Upload/{ID}/{IMAGE_CONTENT_AS_BYTE_ARRAY}
我需要将图像内容作为字节数组字符串发送到此端点方法。但是由于字符长度可以超过2000个字符,如果图像太大,我可能无法发送图像,因为所有内容都是URL字符串的一部分。如何将
图像\u内容\u的数据作为\u字节\u数组
。 此外,我没有任何密钥,所以我可以把它放在namevalue对中。请建议

请尝试以下代码:

MultipartEntityBuilder multipartEntity;

    String URL = "My server url";

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
     Bitmap   bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        bitmap.compress(CompressFormat.JPEG, 75, byteArrayOutputStream); 
        byte[] byteData = byteArrayOutputStream.toByteArray();

        ByteArrayBody byteArrayBody = new ByteArrayBody(byteData, "image"); // second parameter is the name of the image )

        // send the package
        multipartEntity = MultipartEntityBuilder.create();
        multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        multipartEntity.addPart("image", byteArrayBody);

上传图像或文件的最佳方式是使用多部分数据格式。 下面是上传图像的示例代码

public static void postMultiPart(String url, File image) 
{
    final android.net.http.AndroidHttpClient client = android.net.http.AndroidHttpClient.newInstance("sample");
    // enable redirects
    HttpClientParams.setRedirecting(client.getParams(), true);

    final String encoded_url = encodeURL(url);
    final org.apache.http.client.methods.HttpPost post = new org.apache.http.client.methods.HttpPost(encoded_url);


    MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    mpEntity.addPart("profile", new FileBody(image));
    post.setEntity(mpEntity);

    org.apache.http.HttpResponse response;
    try {
        response = client.execute(post);
        final int statusCode = response.getStatusLine().getStatusCode();
        if (!(statusCode == org.apache.http.HttpStatus.SC_OK || statusCode == org.apache.http.HttpStatus.SC_CREATED)) {
            Log.i("Error:","Check....."+"Error " + statusCode + " while posting data to " + encoded_url + "\nreason phrase: " + response.getStatusLine().getReasonPhrase());

            return;
        }

         Log.i("SUCCESS:","Check....."+Base64.encodeToString(md.digest(), Base64.DEFAULT));

    } catch (IOException e) {

    } finally {
        client.close();
    }
}

在SO中不鼓励只使用代码的答案。谢谢您的建议。但在我的情况下,我没有像你这里的“图像”我有简单的restapi占位符{ID}/{image\u CONTENT\u as\u BYTE\u ARRAY}谢谢你的建议。但在我的情况下,我没有像你这里的“配置文件”我有简单的restapi占位符my.domain.com/Upload{ID}/{IMAGE\u CONTENT\u as\u BYTE\u ARRAY}你已经实现了吗?我不清楚你有什么。您已经可以处理非常小的图像了吗?
以字节数组字符串的形式发送图像内容。那到底是什么?请详细说明<代码>{IMAGE\u CONTENT\u AS\u BYTE\u ARRAY}
。但这并没有告诉你要使用字符串。这是我的Web服务团队给我的占位符,用来在剩余的API中发送图像的字节数组。你是认真的吗?请回答我所有的问题和评论。请详细说明你要做什么<代码>如何将图像内容的数据作为字节数组放置。你应该问问给你这个任务的人。