2012-04-20 128 views
0

我在將數據發佈到我的REST WCF服務時遇到了一些困難。我需要向它發送一個json對象/數組,但是我的POST方法正在期待一個Stream,然後將它分開以獲取JSON(不能更改此部分)。使用android/httpclient與流發佈json數據到REST服務

我已經在C#中使用此代碼來實現這一點:

public static string CallPostService(string url, string data) 
    { 
     url = Config.serviceAddress + url; 
     string json = data; 
     byte[] buffer = Encoding.UTF8.GetBytes(json); 
     HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); 
     request.Method = "POST"; 
     request.Credentials = new NetworkCredential("user", "pass"); 
     request.ContentType = "application/x-www-form-urlencoded"; 
     using (StreamWriter sw = new StreamWriter(request.GetRequestStream())) 
     { 
      sw.Write(json); 
      Console.WriteLine(json); 
     } 
     HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
     using (StreamReader sr = new StreamReader(response.GetResponseStream())) 
     { 
      string res = sr.ReadToEnd(); 
      return res; 
     } 
    } 

我需要一些相應的Java代碼,要做到這一點,最好是使用Apache的HttpClient。我是http庫的新手,所以我希望有一個方向。

EDITS:

這是從我的WCF服務的方法頭。請求主體需要是一個流,以便服務可以處理它。

[WebInvoke(Method = "POST", UriTemplate = "person/delete", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] 
    Person DeletePerson(Stream streamdata) { //bla } 
+0

有多遠,你用java到哪裏去了? – njzk2 2012-04-20 15:06:24

+0

不知道你在問什麼,但我實際上已經能夠使用HttpClient執行GET,只需要POST開始工作。 – 2012-04-20 15:08:33

+0

後的工作方式相同,只需要給它一個實體(可以是很多不同的東西) – njzk2 2012-04-20 15:09:24

回答

0

查看此代碼段。 我真的憑着嘗試過,但它應該工作

public void postData() { 
try {  
// Create a new HttpClient and Post Header 
HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php"); 
String auth = android.util.Base64.encodeToString(
(username + ":" + password).getBytes("UTF-8"), 
android.util.Base64.NO_WRAP 
); 
httppost.addHeader("Authorization", "Basic "+ auth); 

    // Add your data 
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
    nameValuePairs.add(new BasicNameValuePair("id", "12345")); 
    nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!")); 
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

    // Execute HTTP Post Request 
    HttpResponse response = httpclient.execute(httppost); 

} catch (ClientProtocolException e) { 
    // TODO Auto-generated catch block 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
} 
} 
+0

這不會起作用。您沒有發送流作爲請求主體。我將發佈一個WCF方法來澄清。 – 2012-04-20 15:12:53

+0

打開HTTP連接和POST字符串 – Aditya 2012-04-20 15:15:16

+0

是的,這就是我想要做的,但我不知道一種方式發送它作爲STREAM .. 你能提供一個例子嗎? – 2012-04-20 15:20:38

0
 String MyData;// Data need to post to server JSON/XML 
     String reply; 
     try { 

     URLConnection connection = url.openConnection(); 
     HttpURLConnection httppost = (HttpURLConnection) connection; 
     httppost.setDoInput(true); 
     httppost.setDoOutput(true); 
     httppost.setRequestMethod("POST"); 
     httppost.setRequestProperty("User-Agent", "UA Here"); 
     httppost.setRequestProperty("Accept_Language", "en-US"); 
     httppost.setRequestProperty("Content-Type", "content type here"); 
     DataOutputStream dos = new DataOutputStream(httppost.getOutputStream()); 
     dos.write(MyData.getBytes()); // bytes[] b of post data 

     InputStream in = httppost.getInputStream(); 
     StringBuffer sb = new StringBuffer(); 
     try { 
      int chr; 
      while ((chr = in.read()) != -1) { 
       sb.append((char) chr); 
      } 
      reply = sb.toString(); 
     } finally { 
      in.close(); 
     } 

     Log.v("POST RESPONSE",reply); 

    } catch (ClientProtocolException e) { 
    } catch (IOException e) { 
    } 
+0

這看起來像我需要的東西,我很困惑你有他們的幾個變量。什麼類型是'urls'和'reply'。 – 2012-04-20 15:26:08

+0

有沒有辦法與http客戶端做到這一點?我必須做NTLM身份驗證,我有自定義的socketfactories和schemefactories這樣做。無論是那種方式或使用HttpURLConnection的方式。 – 2012-04-20 16:06:24

+0

我如何使用這個發送特殊字符? 我試圖用這個幫我connection.setRequestProperty(「Content-Type」,「application/x-www-form-urlencoded; charset = UTF-8」); (「Content-Length」,String.valueOf(strJsonRequest.toString()。getBytes()。length));方法返回一個數組。 – 2012-09-12 12:38:52

相關問題