无法从我的Android应用程序做一个HTTP POST到Web服务 [英] Can't do a HTTP Post to a Web Service from my Android App
问题描述
我一直对使用GET和POST请求的Web服务的应用程序。在GET请求没有问题,但POST请求笑死我了。我试过2个不同的场景会在code。第一个看起来像这样...
I've been working on an app that uses both GET and POST requests to Web Services. The GET requests are no problems but the POST requests are killing me. I've tried 2 different scenarios in the code. The first looks like this...
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(ws);
JSONObject jsonObject = new JSONObject();
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
jsonObject.put("action", "login");
jsonObject.put("username", "*********");
jsonObject.put("password", "*********");
httppost.setHeader("jsonString", jsonObject.toString());
StringEntity se = new StringEntity( jsonObject.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(se);
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
textview.setText(getResponse(response.getEntity()));
} catch (ClientProtocolException e) {
textview.setText(e.getLocalizedMessage());
} catch (IOException e) {
textview.setText(e.getLocalizedMessage());
} catch (JSONException e) {
textview.setText(e.getLocalizedMessage());
}
这code得到这样的结果对我来说...结果
错误的请求(无效标题名称)
This code gets this result for me...
"Bad Request (Invalid Header Name)"
现在这里是我的第二块code ...
Now here's my second piece of code...
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(ws);
JSONObject jsonObject = new JSONObject();
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
jsonObject.put("action", "login");
jsonObject.put("username", "******");
jsonObject.put("password", "******");
nameValuePairs.add(new BasicNameValuePair("jsonString", jsonObject.toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
textview.setText(getResponse(response.getEntity()));
} catch (ClientProtocolException e) {
textview.setText(e.getLocalizedMessage());
} catch (IOException e) {
textview.setText(e.getLocalizedMessage());
} catch (JSONException e) {
}
这给了我一个完全不同的结果。这是XML和SOAP长乱码混乱,确实有它提到一个SOAP异常......结果
服务器无法处理请求--- System.Xml.XmlException:。在根级别的数据无效线路1,位置1结果
现在,任何人都可以阐明什么我做错了任何光线。
This gives me an entirely different result. It's a long garbled mess of xml and SOAP that does have a SOAP Exception mentioned in it...
"Server was unable to process request. --- System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1."
Now, can anyone shed any light on what I am doing wrong.
推荐答案
在您的第二件code附加的:
in your second piece of code add ::
// Execute HTTP Post Request
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters, HTTP.UTF_8);
httppost.setEntity(formEntity);
HttpResponse response = httpclient.execute(httppost);
这篇关于无法从我的Android应用程序做一个HTTP POST到Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!