Android 在webservice中将JSON obj作为参数传递-返回;“错误请求”;响应
Android新手,我需要在我的webservices调用中使用以下参数。我知道参数实际上是JSON对象 下面的代码在返回登录用户信息时返回带有“title:Bad request”的XML。logcat将值显示为-->json:{“查询”:“com.androidatc.customviewindrawer”。Query@f1eb09f“,”includeUserMiscInfo“:true}表示我的参数不正确。如何正确通过Android 在webservice中将JSON obj作为参数传递-返回;“错误请求”;响应,android,json,web-services,Android,Json,Web Services,Android新手,我需要在我的webservices调用中使用以下参数。我知道参数实际上是JSON对象 下面的代码在返回登录用户信息时返回带有“title:Bad request”的XML。logcat将值显示为-->json:{“查询”:“com.androidatc.customviewindrawer”。Query@f1eb09f“,”includeUserMiscInfo“:true}表示我的参数不正确。如何正确通过 protected void sendJson(final Str
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;
}
}
如有任何建议,我们将不胜感激。非常感谢。原因是您没有传递密钥,只是传递值。应该是
somekey = your JSON
通过使用
somekey
,您可以从Web API访问您的JSON
,,我认为您在为HTTPPOST添加实体时有一些困惑。
您需要发送一个JSONArray对象,但实际上,您发送了一个JSon对象。
请参见图片:作为您的Json类型,您可能需要这样做
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");
什么是钥匙?你能进一步解释一下吗。tks.
somekey
是任意键,数据应该以键值对的形式发送,其中somekey
可以是您选择的任何内容。java也可以在代码中。请看看这是不是你的意思?你没有明白我的意思。数据应该像userName=Dep,field=android那样发送。通过这种方式,它应该是关键值,pairI明白了你的意思。logcat将值显示为-->json:{“查询”:“com.androidatc.customviewindrawer”。Query@f1eb09f“,”includeUserMiscInfo“:true}表示我的参数不正确。但是如何正确地传递它呢?也许你是对的,我如何传递JSONArray对象呢?我已经更新了我的答案,试着这样做。但不要复制和粘贴,因为我是直接在编辑器中写的。这很有效!非常感谢你的帮助,非常感谢。被困在这里三天了。