我需要一个有效的示例/教程,了解如何将输入数据从Android传递到.net Web服务以及将结果提取到Android应用程序。将数据传递给.net Web服务并获取结果?
请分享您可能认为与我的需求相关的指南,示例和教程的任何链接。
我需要一个有效的示例/教程,了解如何将输入数据从Android传递到.net Web服务以及将结果提取到Android应用程序。将数据传递给.net Web服务并获取结果?
请分享您可能认为与我的需求相关的指南,示例和教程的任何链接。
您可以创建一个休息web服务,这里是一个tutorial,您可以使用URI传递的输入数据,你应该计划你的URI应该怎样例如张贴客户名称:
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "json/post-customer/{name}")]
void postCustomer(string name);
得到客户数据使用客户ID:
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "json/get-customer/{id}")]
Customer getCustomer(string id);
然后和托管你的web服务后,你需要从你的Android应用程序中使用HTTP客户端访问它,例如:
String uri = "uri to your service";
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet(uri);
ResponseHandler<String> handler = new BasicResponseHandler();
String result = null;
try {
result = httpclient.execute(request, handler);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
httpclient.getConnectionManager().shutdown();
之后,你应该在“结果”中有一个字符串,这个字符串表示你的响应(json或xml)。
希望这个信息可以帮助你。
这里给小样本尝试一下本作后
公共无效后(APPID字符串,字符串custlogid,字符串cityareaid,字符串mosqname,字符串mosqid,纬度字符串,字符串LON){
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(globalconstant.URL_mosquepost);
UrlEncodedFormEntity form;
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(6);
nameValuePairs.add(new BasicNameValuePair("APPUDID", appid));
nameValuePairs.add(new BasicNameValuePair("CUSTLOGID", custlogid));
nameValuePairs.add(new BasicNameValuePair("CITYAREAID", cityareaid));
nameValuePairs.add(new BasicNameValuePair("CITYMOSQUENAME", mosqname));
nameValuePairs.add(new BasicNameValuePair("CITYMOSQUEID", "0"));
nameValuePairs.add(new BasicNameValuePair("LATITUDE", lat));
nameValuePairs.add(new BasicNameValuePair("longitude", lon));
try {
form = new UrlEncodedFormEntity(nameValuePairs,"UTF-8");
httppost.setEntity(form);
Log.d("myapp", "works till here. 2");
try {
HttpResponse response = httpclient.execute(httppost);
Log.d("myapp", "response " + response.getStatusLine().getStatusCode()+"\n"+EntityUtils.toString(response.getEntity()));
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}