package com.bawei.day1.utils; import android.util.Log; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class PostThread extends Thread{ String path; public PostThread(String path) { this.path = path; } @Override public void run() { super.run(); try { URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setConnectTimeout(2000); connection.setReadTimeout(2000); connection.setDoInput(true); connection.setDoOutput(true); connection.connect(); String prams = "stage_id=1&limit=20&page=1"; OutputStream outputStream = connection.getOutputStream(); outputStream.write(prams.getBytes()); outputStream.flush(); if (connection.getResponseCode() == 200){ InputStream inputStream = connection.getInputStream(); byte[] buffer = new byte[1024]; int len = 0; StringBuilder builder = new StringBuilder(); while ((len = inputStream.read(buffer)) != -1 ){ builder.append(new String(buffer, 0, len)); } String s = builder.toString(); Log.i("TAG", s); } } catch (MalformedURLException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } } }
post请求
最新推荐文章于 2024-07-23 21:45:09 发布