他的回复:
new Thread(new Runnable() { @Override public void run() { try { JSONObject body = new JSONObject(); body.put("data", "xxxxx") String urlPath = "给出的模型url"; URL url = null; url = new URL(urlPath); HttpURLConnection conn = null; conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); // 设置允许输出 conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json"); DataOutputStream os = new DataOutputStream( conn.getOutputStream()); String content = String.valueOf(body); os.writeBytes(content); os.flush(); os.close(); if(conn.getResponseCode() == HttpURLConnection.HTTP_OK) { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, "post successfully!", Toast.LENGTH_LONG).show(); } }); InputStreamReader in = new InputStreamReader(conn.getInputStream()); BufferedReader bf = new BufferedReader(in); String recieveData = null; String result = ""; while ((recieveData = bf.readLine()) != null){ result += recieveData + "\n"; } in.close(); conn.disconnect(); } else { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, "post failure!!", Toast.LENGTH_LONG).show(); } }); } } catch (JSONException | MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }})start();用这种方式去连接,无反应,是出什么问题了吗?程序没有报错。