Android新手,webservice調用返回狀態碼400.我懷疑這是由於傳遞參數的方式不正確。我需要將它們作爲JSON對象傳遞,但不知道該怎麼做?如何將webservice參數作爲JSON對象傳遞?
下面應該是參數並正常工作。
在我的Android代碼,其表示狀態代碼400
(更新爲下面的代碼)
RequestParams params = new RequestParams();
Map<String, String> map = new HashMap<String, String>();
map.put("login", "WT"); map.put("password", "03");
params.put("query", map); params.put("includeUserMiscInfo", "true");
client.post("http://XXXX/SDService_SAFTI/ServiceSD.svc/LoginUser",params,new AsyncHttpResponseHandler() {
我也使用如下JSON對象嘗試。
protected void sendJson(final String email, final String pwd) {
Thread t = new Thread() {
public void run() {
Looper.prepare(); //For Preparing Message Pool for the child Thread
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
HttpResponse response;
JSONObject json = new JSONObject();
try {
HttpPost post = new HttpPost("http://192.168.0.102/SDService_SAFTI/ServiceSD.svc/LoginUser");
Query queryObj = new Query();
queryObj.setLogin("WT");
queryObj.setPassword("3");
json.put("Query", queryObj);
// json.put("email", email);
// json.put("password", pwd);
json.put("includeUserMiscInfo", true);
StringEntity se = new StringEntity(json.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(se);
response = client.execute(post);
/*Checking response */
if(response!=null){
InputStream in = response.getEntity().getContent(); //Get the data in the entity
Toast.makeText(getActivity().getApplicationContext(), "Response:" + convertStreamToString(in),Toast.LENGTH_LONG).show();
}
} catch(Exception e) {
e.printStackTrace();
// getActivity().createDialog("Error", "Cannot Estabilish Connection");
}
Looper.loop(); //Loop in the message queue
}
};
t.start();
}
private static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append((line + "\n"));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
但它返回 - 請求錯誤XML
任何建議都歡迎。提前謝謝了。
我的問題在這裏得到解決 - > http://stackoverflow.com/questions/35738165/passing-json-obj-as-parameter-in-webservice-returns-bad-request-response/35738591#35738591 – Dep