httpClient之Post请求

xiaoxiao2021-02-27  458

public class MainActivity extends AppCompatActivity {     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);     }     public void btnHttpClientPost(View v){         new Thread(){             @Override             public void run() {                 super.run();                 httpClientPostReqeust("ed1088c0c4ac34c42a270fabf0de9b5a");             }         }.start();     }     /**      *      * httpClietnt API 的pOST方式拉去数据      *      */     private String httpClientPostReqeust(String key) {         try {             //默认 取消对HttpClient的一个支持             //1.打开浏览器             HttpClient httpClient = new DefaultHttpClient();             //2.填下地址             HttpPost httpPost = new HttpPost("http://v.juhe.cn/WNXG/city");             //httpPost.addHeader("token","123544");             //设置请求参数             List<BasicNameValuePair> paramters = new ArrayList<>();             //只是添加一个了参数             //paramters.add(new BasicNameValuePair("menu", menu));             paramters.add(new BasicNameValuePair("key", key));             httpPost.setEntity(new UrlEncodedFormEntity(paramters));             //key=43434&name=zhangsan             //3.回车             HttpResponse httpResponse = httpClient.execute(httpPost);             //等待服务器响应             //200  成功  404 资源没找到  500 服务内部错误             int code = httpResponse.getStatusLine().getStatusCode();             if (code == HttpStatus.SC_OK) {                 //获取服务的响应内容                 InputStream is = httpResponse.getEntity().getContent();                 String json = StreamTools.readFromNetWork(is);                 System.out.println("http post 请求结果 : " + json);                 return json;             }         } catch (Exception e) {             e.printStackTrace();         }         return null;     }
转载请注明原文地址: https://www.6miu.com/read-814.html

最新回复(0)