2


私は何日も戦っており、すでに多くの例を試しましたが、解決できず、混乱しているため、重複としてマークしないでください.WCFとAndroidも初めてな
ので、いくつかの取得と以下のような投稿方法

[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) から customdatatype を返したいのですが、何か特別なことをする必要がありますか?

すでに多くの例を試しましたが、解決できず混乱しているため、重複としてマークしないでください。

私を助けてください!!!どうぞよろしくお願いいたします。
よろしく、
Sourabh

4

1 に答える 1

0

メディア ファイル (または任意のファイル) を WCF に送信するには、要求本文の唯一のパラメーターが型である操作が必要Streamです。つまり、操作に他のパラメーターを指定できますが、URI 経由で (属性のUriTemplateプロパティを使用して[WebInvoke]) 渡す必要があります。詳細については、http://blogs.msdn.com/b/carlosfigueiraの投稿を参照してください。 /archive/2008/04/17/wcf-raw-programming-model-receiving-arbitrary-data.aspx .

あなたの例では、以下のコードのようなものがあります。

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

また、クライアントでは JSON を使用せずに、ファイル以外のパラメーターを要求 URI に渡し、ファイルの内容を要求本文に渡します。

そして、あなたの他の質問について:はい、それはビデオファイルでも機能します(さらに言えば、任意のデータの場合)。いいえ、戻り値の型に対して特別なことをする必要はありません - それはうまくいくはずです。

于 2013-06-27T20:43:20.940 に答える