2013-06-27 29 views
2


請不要將其標記爲重複的爲我爭取了很多天,並已經嘗試過很多的例子,但不能解決並得到confuse.I是新來的WCF和Android也
所以我創建了一個WCF服務的一些get和post方法如下面如何發送媒體文件WCF休息JSON服務,並得到JSON格式的字符串返回響應

[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

回答

0

要發送媒體文件(或任意文件,爲這一問題)到WCF,你需要有一個操作,其中,在請求體的唯一參數Stream類型。這意味着您可以爲操作提供其他參數,但需要通過URI傳遞(使用[WebInvoke]屬性的UriTemplate屬性) - 請參閱文章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通過非文件的參數,以及文件內容在請求正文中。

對於您的其他問題:是的,它也可以用於視頻文件(對於任何任意數據,就此而言);不,你不需要爲返回類型做任何特殊的事情 - 它應該工作。

+0

感謝您的回覆!它幫助了我。我嘗試沒有圖像(只有網址參數),它正在工作。
但是當我嘗試與請求正文中的圖像它不工作。我確信我在將圖片放在http post中時出錯。這是我使用的代碼。 – user2376920

+0

請忽略以上評論。 感謝您的回覆!我試過沒有圖像,然後它正在工作。但是當我嘗試與請求身體中的圖像它不工作。這是我使用的代碼。 ** MultipartEntity mpEntity = new MultipartEntity(); File file = new File(imagepath); ContentBody cbFile = new FileBody(file,「image/jpeg」); mpEntity.addPart(「image」,cbFile); httpPost.setEntity(mpEntity); ** 請告訴我如何在http請求正文中發送圖片。我從其他教程獲得以上代碼。如果你可以用一點解釋告訴我確切的編碼,它會更有幫助。 感謝和問候, Sourabh – user2376920

+0

您不應該使用'MultipartEntity' - 而是使用'ByteArrayEntity'。服務器中的'Stream'參數將接收客戶端發送的字節,並且您希望從文件發送確切的字節。 – carlosfigueira

相關問題