简体   繁体   English

使用Java(特别是Android)发布到REST API

[英]Posting to REST api using Java, specifically Android

I'm posting to the Wufoo api inside of an Android app and I am hitting a bit of a snag. 我在Android应用程序内部发布到Wufoo api,但遇到了一些麻烦。 My data does not seem to be formatting in a way that the server likes (or there is some other issue). 我的数据似乎没有按照服务器喜欢的方式进行格式化(或存在其他问题)。 Here is my code (note authkey and authpass are placeholders in the exmaple): 这是我的代码(注意authkey和authpass是示例中的占位符):

HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);

String json = "";

JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("Field17", "Some Value");

json = jsonObject.toString();

StringEntity postData = new StringEntity(json, "UTF8");

httpPost.setEntity(postData);

String authorizationString = "Basic " + Base64.encodeToString(
("authkey" + ":" + "authpass").getBytes(),
Base64.NO_WRAP);

httpPost.setHeader("Content-type", "application/json");
httpPost.setHeader("Authorization", authorizationString);

HttpResponse httpResponse = httpclient.execute(httpPost);

inputStream = httpResponse.getEntity().getContent();

The response I get back from the server looks like this: 我从服务器获得的响应如下所示:

{"Success":0,"ErrorText":"Errors have been <b>highlighted<\/b> below.","FieldErrors":
[{"ID":"Field17","ErrorText":"This field is required. Please enter a value."}]}

This is the response for a failure (obviously) which leads me to believe I'm doing the authentication correctly, and that it just doesn't like my JSON string, I've looked through the API docs which are located here: http://www.wufoo.com/docs/api/v3/entries/post/ 这是对失败的响应(很明显),这使我相信我正在正确地进行身份验证,并且它只是不喜欢我的JSON字符串,因此我浏览了位于以下位置的API文档: http: //www.wufoo.com/docs/api/v3/entries/post/

and by all accounts this should work? 并且从所有方面来说,这应该起作用吗? Any suggestions? 有什么建议么?

I would start by looking at this line: 我将从看这行开始:

StringEntity postData = new StringEntity(json, "UTF8");

It's "UTF-8" , not "UTF8" . 它是"UTF-8" ,而不是"UTF8"

Note : I would suggest you using the HTTP.UTF_8 constant in order to avoid this kind of problem again. 注意 :我建议您使用HTTP.UTF_8常量,以避免再次发生此类问题。

StringEntity postData = new StringEntity(json, HTTP.UTF_8);

除了字符串,Field17可以是特定的字段类型。

After reading the document, I think you missed the point. 阅读文档后,我认为您错过了重点。 The server accepted fields parameter from http post, not from a json string. 服务器从http发布而不是从json字符串接受fields参数。
Your problem looks like this one . 您的问题看起来像这样 So your request should like this: 因此,您的请求应如下所示:

ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("Field17", "Some Value"));

httpPost .setEntity(new UrlEncodedFormEntity(postParameters));

Hope this can help. 希望这会有所帮助。

I actually figured this out, this isn't a problem with the code anyone here gave me, it's the fact that I was sending the wrong header info. 我实际上已经弄清楚了,这里的任何人给我的代码都不是问题,这是我发送错误的标头信息的事实。 This must be a quirk of the Wufoo API. 这必须是Wufoo API的怪癖。

If I use the BasicNameValuePair objects like what was suggestion by R4j and I remove the line 如果我像R4j那样使用BasicNameValuePair对象,则删除该行

httpPost.setHeader("Content-type", "application/json");

everything works perfectly! 一切正常!

Thanks for all the help and I hope this helps anyone who is having trouble with the Wufoo API and Java. 感谢您提供的所有帮助,希望对任何对Wufoo API和Java有困难的人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM