Java Android:-通过Post方法将web服务作为json请求和响应使用
我的web服务代码如下:我正在使用WCF Restful web服务Java Android:-通过Post方法将web服务作为json请求和响应使用,java,android,json,web-services,wcf,Java,Android,Json,Web Services,Wcf,我的web服务代码如下:我正在使用WCF Restful web服务 [OperationContract] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "Login?parameter={parameter}")] string Logi
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "Login?parameter={parameter}")]
string Login(string parameter);
public string Login(string parameter)
{
/*
* input := {"username":"kevin","password":"123demo"}
* output:= 1=sucess,0=fail
*
*/
//Getting Parameters from Json
JObject jo = JObject.Parse(parameter);
string username = (string)jo["username"];
string password = (string)jo["password"];
return ""+username;
}
我的客户端(Android)代码如下
JSONObject json = new JSONObject();
try {
json.put("username","demo");
json.put("password","password123");
HttpPost postMethod = new HttpPost(SERVICE_URI);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
postMethod.setHeader("Accept", "application/json");
postMethod.setHeader("Content-type", "application/json");
nameValuePairs.add(new BasicNameValuePair("parameter",""+json.toString()));
HttpClient hc = new DefaultHttpClient();
postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = hc.execute(postMethod);
Log.i("response", ""+response.toString());
HttpEntity entity = response.getEntity();
final String responseText = EntityUtils.toString(entity);
string=responseText;
Log.i("Output", ""+responseText);
}
catch (Exception e) {
// TODO Auto-generated catch block
Log.i("Exception", ""+e);
}
JSONObject json=new JSONObject();
试一试{
put(“用户名”、“演示”);
put(“密码”、“密码123”);
HttpPost postMethod=新的HttpPost(服务URI);
List nameValuePairs=新的ArrayList();
setHeader(“接受”、“应用程序/json”);
setHeader(“内容类型”、“应用程序/json”);
添加(新的BasicNameValuePair(“参数“,”+json.toString());
HttpClient hc=新的默认HttpClient();
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=hc.execute(postMethod);
Log.i(“response”,“”+response.toString());
HttpEntity=response.getEntity();
最终字符串responseText=EntityUtils.toString(实体);
字符串=响应文本;
Log.i(“输出”,“响应文本”);
}
捕获(例外e){
//TODO自动生成的捕捉块
Log.i(“例外情况”和“+e”);
}
调用Web服务后,我得到以下输出:
服务器在处理请求时遇到错误。请参阅服务器
日志以获取更多详细信息
基本上,我的问题是我无法通过使用
NameValuePair
以下代码传递值:
public static String getJsonData(String webServiceName,String parameter)
{
try
{
String urlFinal=SERVICE_URI+"/"+webServiceName+"?parameter=";
HttpPost postMethod = new HttpPost(urlFinal.trim()+""+URLEncoder.encode(parameter,"UTF-8"));
postMethod.setHeader("Accept", "application/json");
postMethod.setHeader("Content-type", "application/json");
HttpClient hc = new DefaultHttpClient();
HttpResponse response = hc.execute(postMethod);
Log.i("response", ""+response.toString());
HttpEntity entity = response.getEntity();
final String responseText = EntityUtils.toString(entity);
string=responseText;
Log.i("Output", ""+responseText);
}
catch (Exception e) {
}
return string;
}
尝试检查原始请求,并查看导致问题的请求中的错误。还可以对您的服务进行跟踪,以了解错误的确切细节。@Rajesh实际上我在服务器上调试原始请求,但工作正常。