android studio http 访问,android studio HttpURLConnection请求出问题

用HttpURLConnection请求百度首页成功,(GET请求)

请求这个地址也成功:http://10.207.112.191:8080,说明后台服务器可用

但请求这个地址不成功:http://10.207.112.191:8080/user

/user后台测试代码:

用的是SpringMVC

@RequestMapping(value = "/user", method = RequestMethod.GET)

public void Reply(HttpServletResponse response) throws IOException {

PrintWriter out = response.getWriter();

System.out.println("来自安卓");

out.print("OK");

out.flush();

out.close();

}

logcat:

D/NetworkSecurityConfig: No Network Security Config specified, using platform default

D/Tag: 未进入http

W/System.err: java.io.FileNotFoundException: http://10.207.112.191:8080/user

W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:250)

W/System.err:     at hope.HttpUtil.HttpUtil$1.run(HttpUtil.java:43)

W/System.err:     at java.lang.Thread.run(Thread.java:761)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的示例代码: ```java // 注册请求 public void register(String username, String password) { try { URL url = new URL("http://example.com/register.php"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setConnectTimeout(5000); conn.setReadTimeout(5000); OutputStream os = conn.getOutputStream(); String data = "username=" + URLEncoder.encode(username, "UTF-8") + "&password=" + URLEncoder.encode(password, "UTF-8"); os.write(data.getBytes()); os.flush(); os.close(); int responseCode = conn.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { // 注册成功 } else { // 注册失败 } conn.disconnect(); } catch (Exception e) { e.printStackTrace(); } } // 登录请求 public void login(String username, String password) { try { URL url = new URL("http://example.com/login.php"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setConnectTimeout(5000); conn.setReadTimeout(5000); OutputStream os = conn.getOutputStream(); String data = "username=" + URLEncoder.encode(username, "UTF-8") + "&password=" + URLEncoder.encode(password, "UTF-8"); os.write(data.getBytes()); os.flush(); os.close(); int responseCode = conn.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { // 登录成功 } else { // 登录失败 } conn.disconnect(); } catch (Exception e) { e.printStackTrace(); } } ``` 以上代码中,register() 方法用于发送注册请求,login() 方法用于发送登录请求。其中,需要替换的部分是请求的 URL 和参数,以及请求成功后的处理逻辑。注意,这里使用了 HttpURLConnection 类进行请求

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值