将图像文件发布到android的wcf rest服务

时间:2013-06-30 07:40:17

标签: android image wcf http-post

请不要将其标记为重复。我尝试了很多例子,但问题没有解决。
我是android的新手,想把图像文件发送给wcf服务。
我正在尝试使用以下代码:
致电后台任务

servicemethodname = "RegisterUser?EmailId=" + emailID + "&Name=" + name
                + "&Mobile=" + mobile + "&IMEI=" + imei;
    DownloadWebPageTask bcktask = new DownloadWebPageTask();
        bcktask.execute(servicemethodname, path,bMap);

安卓代码

package com.example.Wcfconsumer1;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.os.AsyncTask;
import android.util.Log;

public class DownloadWebPageTask extends AsyncTask<Object, Integer, String> {


    private final static String SERVICE_URI = "http://192.168.0.102:80/Service1.svc/";

    protected void onPostExecute(String result) {
        MainActivity.emailV.setText(result);
    }

    @Override
    protected String doInBackground(Object... params) {
        Bitmap image = (Bitmap)params[2];
        String msg = new String();
        String methodname = params[0].toString();
         try {
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                image.compress(CompressFormat.JPEG, 75, bos);
                byte[] data = bos.toByteArray();
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost postRequest = new HttpPost(SERVICE_URI+methodname);
                ByteArrayBody bab = new ByteArrayBody(data, "forest.jpg");
                // File file= new File("/mnt/sdcard/forest.png");
                // FileBody bin = new FileBody(file);
                MultipartEntity reqEntity = new MultipartEntity(
                        HttpMultipartMode.BROWSER_COMPATIBLE);
                reqEntity.addPart("uploaded", bab);
                reqEntity.addPart("photoCaption", new StringBody("sfsdfsdf"));
                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);
                }
                System.out.println("Response: " + s);
            } catch (Exception e) {
                Log.e(e.getClass().getName(), e.getMessage());
            }
        return msg;
    }
}

和wcf如下:

 [OperationContract]
        [WebInvoke(Method = "POST",
           UriTemplate = "RegisterUser?EmailId={EmailID}&Name={Name}&Mobile={Mobile}&IMEI={IMEI}",
           //BodyStyle = WebMessageBodyStyle.WrappedRequest,
           //RequestFormat= WebMessageFormat.Json,
           ResponseFormat = WebMessageFormat.Json)]
        string RegisterUser(string EmailID, string Name, string Mobile, long IMEI,Stream Profilepic);

当我尝试在请求体中发布图像时,我发现执行方法失败了。 请帮我完成这件事。我已经尝试了很多选项和示例,但不知道为什么它没有完成。

1 个答案:

答案 0 :(得分:0)


谢谢carlosfigueira回复!!!
但我想告诉我的是代码工作正常而且我正在做的错误是我在“String s”中收集了响应但是将“String msg”返回到“onPostExecute”方法以在视图中设置它因此我不是得到结果。所以这段代码很好而且正确。


谢谢&amp;问候,
Sourabh