我需要一个有效的例子/教程如何传递输入数据从安卓到.net网络服务和检索结果到安卓应用程序。
请分享任何您可能觉得与我的需求相关的指南、示例和教程的链接。
发布于 2012-05-06 15:35:12
你可以创建一个Rest Webservice,这是一个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);然后,在托管您的for服务之后,您需要使用HTTP客户端从android应用程序访问它,例如:
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();在此之后,您应该在"result“中有一个字符串,该字符串表示您的响应(json或xml)。
希望这些信息能帮助你。
发布于 2012-06-03 22:42:22
这里给出了一个小样本,试着在post中使用
public void post(String appid,String custlogid,String cityareaid,String mosqname,String mosqid,String lat,String 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();
}
}https://stackoverflow.com/questions/10468808
复制相似问题