2012-04-20 16 views
0

REST WCFサービスにデータをPOSTするのが難しいです。 jsonオブジェクト/配列を送る必要がありますが、私のPOSTメソッドは、JSON(この部分を変更することはできません)を取得するために離れて選択されたStreamを予期しています。ストリームでアンドロイド/ 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; 
     } 
    } 

私は、好ましくはApacheのHttpClientを使用して、これを行うには、いくつかの同等のJavaコードが必要です。私はhttpライブラリには新しく、少しの方向性に感謝します。

EDITS:

は、ここに私のWCFサービスからのメソッドヘッダです。要求本体はストリームでなければならないので、サービスはそれを処理できます。

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

を動作するはずですDINT – njzk2

+0

あなたが何を求めているのか分かりませんが、実際にはHttpClientでGETを実行できました。 –

+0

投稿は同じ方法で動作します。エンティティには多くの異なるものがあります。 – njzk2

答えて

0

このコードスニペットを確認してください。 私は実際にそれを試してみましたが、それはどこまであなたがJavaを使用して行っている?

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メソッドを投稿します。 –

+0

HTTP接続とPOST文字列を開く – Aditya

+0

はい、これはやりたいことですが、ストリームとして送信する方法はわかりません。 例を挙げてください。 –

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

これは私が必要とするもののように見えますが、私はあなたが彼らの持っている変数について混乱しています。どのようなタイプの 'URL'と '返信'です。 –

+0

httpクライアントでこれを行う方法はありますか?私はNTLM認証をしなければなりません。私はカスタムソケットファクトリとそれを行うための計画を持っています。どちらか、またはHttpURLConnectionでこれらを使用する方法。 –

+0

これを使用して特殊文字を送信するにはどうすればよいですか? 私はconnection.setRequestProperty( "Content-Type"、 "application/x-www-form-urlencoded; charset = UTF-8")を助けてくれました。 \t \t \t connection.setRequestProperty( "Content-Length"、String.valueOf(strJsonRequest.toString()。getBytes()。length)); –

関連する問題