1

请不要将其标记为重复。我尝试了很多例子,但问题没有解决。
我是 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);

我观察到的一件事是,当我尝试在请求正文中发布图像时,执行方法失败。请帮我完成这件事。我尝试了很多选项和示例,但不知道为什么没有完成。

4

1 回答 1

0


谢谢卡洛斯菲盖拉的回复!!!
但我想说的是代码工作正常,我犯的错误是我在“String s”中收集了响应,但将“String msg”返回到“onPostExecute”方法以将其设置在视图中,因此我没有得到结果。所以这段代码很好而且正确。


谢谢和问候,
苏拉布

于 2013-06-30T18:44:26.043 回答