Android新手,我需要在我的webservices調用中使用下面的參數。我明白參數實際上是JSON對象。在webservice中傳遞JSON obj作爲參數 - 返回「Bad Request」響應
下面的代碼返回XML與「標題:壞請求」時,它應該返回登錄的用戶信息。 logcat顯示值爲 - > json:{「Query」:「[email protected]」,「includeUserMiscInfo」:true}表示我的參數不正確。如何正確傳遞它?
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();
}
Query.java
public class Query {
String login;
public void setPassword(String password) {
this.password = password;
}
public void setLogin(String login) {
this.login = login;
}
String password;
public String getPassword() {
return password;
}
public String getLogin() {
return login;
}
}
任何建議都理解的。提前謝謝了。
我已更新我的答案是這樣的。但不要複製和粘貼,因爲我直接在編輯器中編寫。 –