请不要将其标记为重复,因为我已经战斗了很多天并且已经尝试了很多示例但无法解决并感到困惑。我也是 WCF 和 android 的新手
所以我创建了一个带有一些 get 的 WCF 服务和发布方法如下
[OperationContract]
[WebInvoke(Method = "POST",
UriTemplate = "RegisterUser",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat= WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
ResultSet RegisterUser(string EmailID, string Name,Stream profilepic, string Mobile, long IMEI);
我正在通过android客户端调用此服务方法,如下所示
MainActivity.java
public void doneOnClick(View v) throws FileNotFoundException,
InterruptedException, JSONException {
// Toast toast = new Toast(this);
// gets IMEI of device ID
tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
imei = tm.getDeviceId();
bMap = BitmapFactory.decodeFile(selectedImagePath);
path = SaveImage.writeFileToInternalStorage(getApplicationContext(),
bMap, "UserImage.png");
name = nameV.getText().toString();
mobile = mobileV.getText().toString();
emailID = emailV.getText().toString();
if (name.length() != 0 && mobile.length() != 0 && emailID.length() != 0
&& path.length() != 0) {
SharedPreferences shared = getSharedPreferences(PREFS, 0);
Editor editor = shared.edit();
editor.putString("UserPicPath", path);
editor.putString("UserName", name);
editor.putString("UserMobile1", mobile);
editor.putString("UserEmail", emailID);
editor.putString("IMEI", imei);
editor.commit();
}
JSONArray jsonarr = new JSONArray();
JSONObject jsonObj = new JSONObject();
jsonObj.put("emailID", emailID);
jsonObj.put("name", name);
jsonObj.put("mobile", mobile);
jsonObj.put("imei", imei);
jsonarr.put(jsonObj);
servicemethodname = "RegisterUser";
DownloadWebPageTask bcktask = new DownloadWebPageTask();
bcktask.execute(servicemethodname, jsonarr);
}
并将 backgroundtask 称为
package com.example.wcfconsumer;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.util.Base64;
import android.util.Log;
public class DownloadWebPageTask extends AsyncTask<Object, Integer, String> {
private final static String SERVICE_URI = "http://192.168.0.100:80/Service1.svc/";
protected void onPostExecute(String result) {
MainActivity.emailV.setText(result);
}
@Override
protected String doInBackground(Object... params) {
JSONArray jsonparams = (JSONArray) params[1];
String methodname = params[0].toString();
InputStream is;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(SERVICE_URI + methodname);
StringEntity se = new StringEntity(jsonparams.toString(), "UTF-8");
se.setContentType("application/json;charset=UTF-8");
httpPost.setEntity(se);
Log.e("Gerhard", jsonparams.toString());
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
InputStreamReader i = new InputStreamReader(is);
BufferedReader str = new BufferedReader(i);
String msg = str.readLine();
Log.e("Gerhard", msg);
return msg;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private String convertToString(Bitmap image) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, bos);
byte[] data = bos.toByteArray();
String mediaString = Base64.encodeToString(data, Base64.URL_SAFE);
return mediaString;
}
}
我的问题包含多个部分:
1. 如何将图像文件与其他数据类型一起发送到 RegisterUser 方法并以 json 格式获得响应?
2. 视频文件和图像文件一样吗?
3. 我想从服务中返回自定义数据类型(在本例中为 ResultSet),我需要为此做些什么特别的事情吗?
请不要将其标记为重复,因为我已经尝试了很多示例但无法解决和混淆。
请帮我!!!很多很多很多提前感谢。
问候,
苏拉布