Android新手,我需要在我的webservices调用中使用以下参数。我知道参数实际上是JSON对象。
下面的代码返回带有"标题:错误请求"的XML。什么时候它应该返回登录的用户信息。 logcat显示值为 - > json:{"查询":" com.androidatc.customviewindrawer.Query@f1eb09f"," 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;
}
}
任何建议都表示赞赏。非常感谢提前。
答案 0 :(得分:1)
我认为在为HTTPPOST添加实体时会有一些混淆。 您需要发送一个JSONArray对象,但事实上,您发送了一个JSon对象。 请参阅图片:http://prntscr.com/aa49cj
答案 1 :(得分:1)
作为你的Json Type,你可能需要这样做,
JSONArray array = new JSONArray();
array.put("login=WT&password=3");
json.put("Query", array);
json.put("includeUserMiscInfo", "true");
我想建议您尝试更换此行,
json.put("includeUserMiscInfo", true);
<强>与强>
json.put("includeUserMiscInfo", "true");
答案 2 :(得分:0)
原因是你没有传递密钥,只是传递价值。它应该是
somekey = your JSON
使用somekey
,您可以从Web API访问JSON