开发者社区> 问答> 正文

Android端和后台.NET通讯json连接异常,希望大家指点

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

急需大家指点呀,谢啦

展开
收起
杨冬芳 2016-07-05 08:52:11 2487 0
1 条回答
写回答
取消 提交回答
  • IT从业

    ,openConnection后,要调用connect方法才能建立连接。
    HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.connect();//加上这句,如果地址没问题的话,

    2019-07-17 19:51:08
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载