使用android/httpclient与流发布json数据到REST服务
我在将数据发布到我的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 }
查看此代码段。 我真的凭着尝试过,但它应该工作
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
}
}
这不会起作用。您没有发送流作为请求主体。我将发布一个WCF方法来澄清。 – 2012-04-20 15:12:53
打开HTTP连接和POST字符串 – Aditya 2012-04-20 15:15:16
是的,这就是我想要做的,但我不知道一种方式发送它作为STREAM .. 你能提供一个例子吗? – 2012-04-20 15:20:38
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) {
}
这看起来像我需要的东西,我很困惑你有他们的几个变量。什么类型是'urls'和'reply'。 – 2012-04-20 15:26:08
有没有办法与http客户端做到这一点?我必须做NTLM身份验证,我有自定义的socketfactories和schemefactories这样做。无论是那种方式或使用HttpURLConnection的方式。 – 2012-04-20 16:06:24
我如何使用这个发送特殊字符? 我试图用这个帮我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
有多远,你用java到哪里去了? – njzk2 2012-04-20 15:06:24
不知道你在问什么,但我实际上已经能够使用HttpClient执行GET,只需要POST开始工作。 – 2012-04-20 15:08:33
后的工作方式相同,只需要给它一个实体(可以是很多不同的东西) – njzk2 2012-04-20 15:09:24