我一直在研究将GET和POST请求同时用于Web服务的应用程序。 GET请求没有问题,但POST请求正在杀死我。我已经在代码中尝试了两种不同的场景。第一个看起来像这样...无法通过我的Android应用程序对Web服务执行HTTP发布
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());
}
这段代码获得这个结果对我来说...
“错误的请求(无效标题名称)”
现在,这里是我的第二一段代码。 ..
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“。
现在,任何人都可以阐明我做错了什么。
添加该位让我得到了相同的XML SOAP错误。 – huffmaster 2010-09-08 12:07:32