Android端代码:
public class HttpUtil {
public static final String BASE_URL = "http://192.168.31.146:23062/";
}
public ProductDetail queryproductsproductsId(int productsId)
{
String path = HttpUtil.BASE_URL+"ToysRent/Product/ProductDetail?Id="+productsId+"";
URL url;
try {
url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(5000);
conn.setRequestMethod("POST");
[color=#FF0000] if (200 == conn.getResponseCode())[/color]
{
//获取输入流
InputStream is = conn.getInputStream();
ToolsHandler toolsHandler=new ToolsHandler();
byte[] data=toolsHandler.InputStreamToByte(is);
json=new String(data);
System.out.println(json);
product =getGson().fromJson(json, new TypeToken() {}.getType());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return product;
}
配置文件:
.NET后台代码:
路径详情,红色圈出
[img=http://img.bbs.csdn.net/upload/201504/30/1430393337_919353.png][/img]
[HttpPost]
public ActionResult ProductDetail(int id)
{
Products product=_productService.GetModel(id);
// 向客户端输出返回信息json数据
if (product != null) {
JsonResult jsr = Json(product);
return jsr;
} else {
return Json("error");
}
}
debug时在Android红色出出现异常:failed to connect to /192.168.31.146 (port 23062): connect failed: ETIMEDOUT
急需大家指点呀,谢啦
,openConnection后,要调用connect方法才能建立连接。
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.connect();//加上这句,如果地址没问题的话,
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。