1
請不要將其標記爲重複。我嘗試過很多例子,但問題沒有解決。
我是Android新手,想發送圖片文件到wcf服務。
我想這與下面的代碼:
調用後臺任務將圖像文件發佈到Android的wcf休息服務
servicemethodname = "RegisterUser?EmailId=" + emailID + "&Name=" + name
+ "&Mobile=" + mobile + "&IMEI=" + imei;
DownloadWebPageTask bcktask = new DownloadWebPageTask();
bcktask.execute(servicemethodname, path,bMap);
的Android代碼
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我已經觀察到,當我嘗試在請求正文中發佈圖像時,execute方法失敗。 請幫我完成這件事。我嘗試了很多選項和例子,但不知道爲什麼它沒有完成。
怎樣的方法失敗? – carlosfigueira
如果您有工作項目文件,請您寄給我,因爲我也在嘗試做同樣的事情。 –
好老兄!我切換到web服務,現在不使用WCF。 – user2376920