Android 如何将webservice参数作为JSON对象传递?
Android新增的webservice调用返回状态代码400。我怀疑这是由于传递参数的方式不正确造成的。我需要将它们作为JSON对象传递,但不确定如何才能做到这一点 下面应该是参数,并且工作正常 在我的Android代码中,它显示状态代码400 (更新代码如下) 但是它返回了请求错误xmlAndroid 如何将webservice参数作为JSON对象传递?,android,json,web-services,Android,Json,Web Services,Android新增的webservice调用返回状态代码400。我怀疑这是由于传递参数的方式不正确造成的。我需要将它们作为JSON对象传递,但不确定如何才能做到这一点 下面应该是参数,并且工作正常 在我的Android代码中,它显示状态代码400 (更新代码如下) 但是它返回了请求错误xml 欢迎提出任何建议。非常感谢。您确定您的urlhttp://192.168.0.102/DService/ServiceSD.svc/LoginUser?Query=login=WT&password=03
欢迎提出任何建议。非常感谢。您确定您的url
http://192.168.0.102/DService/ServiceSD.svc/LoginUser?Query=login=WT&password=03&includeUserMiscInfo=true
是否正确?您可以使用以下方法调用您的web服务:
public void makeHTTPCall() {
RequestParams params = new RequestParams();
String query = "[login=WT&password=03]"
params.put("query", query);
params.put("includeUserMiscInfo", "true");
prgDialog.setMessage(getResources().getString(R.string.please));
AsyncHttpClient client = new AsyncHttpClient();
client.post("http://192.168.0.102/SDService_SAFTI/ServiceSD.svc/LoginUser",
params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
// Hide Progress Dialog
prgDialog.hide();
// do your code if result is success
}
@Override
public void onFailure(int statusCode, Throwable error,
String content) {
prgDialog.hide();
if (statusCode == 404) {
Toast.makeText(getApplicationContext(),
"Requested resource not found",
Toast.LENGTH_LONG).show();
}
else if (statusCode == 500) {
Toast.makeText(getApplicationContext(),
"Something went wrong at server end",
Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(
getApplicationContext(),
getResources().getString(R.string.some_error_occured)
, Toast.LENGTH_LONG)
.show();
}
}
});
}
希望这对您有所帮助。是正确的。我不知道如何传递参数?我也尝试过使用Requestparams。请查看高级客户端url(正确)。您的更新代码看起来正确。您是否仍然收到400错误或其他错误(logcat中的任何错误)?仍然相同的状态代码=400。请求错误。Android监视器上没有错误。请看一看,您可能需要结合一些方法map=new HashMap();地图放置(“登录”、“WT”);地图放置(“密码”、“03”);参数put(“用户”,映射);//url参数:“用户[名字]=James和用户[姓氏]=Smith”参数put(“查询”,映射);参数put(“includeUserMiscInfo”、“true”);KishuDroid,看截图。我的webservices参数没有那么简单。查询中包含2项-登录名和密码。includeUserMiscInfo是另一个参数。实际上,您传递的格式不正确。所以试着像这样通过它。查看我的编辑我使用的正是您提到的:RequestParams params=newrequestparams();字符串查询=“[login=WT&password=03]”参数放置(“查询”,查询);参数put(“includeUserMiscInfo”、“true”);参数put(“用户”,映射);参数put(“查询”,map);这是什么.sry参数.put(“用户”,映射);这是额外的。更新了代码。但还是一样的错误。我的问题在这里得到解决-->
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();
}
public void makeHTTPCall() {
RequestParams params = new RequestParams();
String query = "[login=WT&password=03]"
params.put("query", query);
params.put("includeUserMiscInfo", "true");
prgDialog.setMessage(getResources().getString(R.string.please));
AsyncHttpClient client = new AsyncHttpClient();
client.post("http://192.168.0.102/SDService_SAFTI/ServiceSD.svc/LoginUser",
params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
// Hide Progress Dialog
prgDialog.hide();
// do your code if result is success
}
@Override
public void onFailure(int statusCode, Throwable error,
String content) {
prgDialog.hide();
if (statusCode == 404) {
Toast.makeText(getApplicationContext(),
"Requested resource not found",
Toast.LENGTH_LONG).show();
}
else if (statusCode == 500) {
Toast.makeText(getApplicationContext(),
"Something went wrong at server end",
Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(
getApplicationContext(),
getResources().getString(R.string.some_error_occured)
, Toast.LENGTH_LONG)
.show();
}
}
});
}