zoukankan      html  css  js  c++  java
  • android:http

    使用org.apache.http

    //生成一个请求对象
    HttpGet httpGet = new HttpGet("http://www.baidu.com");
    //生成一个Http客户端对象
    HttpClient httpClient = new DefaultHttpClient();
    //使用Http客户端发送请求对象
    InputStream inputStream = null;
    try {
        httpResponse = httpClient.execute(httpGet);
        httpEntity = httpResponse.getEntity();
        inputStream = httpEntity.getContent();
        //文件流操作
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        String result = "";
        String line = "";
    //  StringBuilder builder = new StringBuilder();
      
    while((line = reader.readLine()) != null){ result = result + line;
    // builder.append(s); } System.out.println(result); }
    catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ try{ inputStream.close(); } catch(Exception e){ e.printStackTrace(); } } }
    /*  JSONObject jsonObject = new JSONObject(builder.toString());
            String re_username = jsonObject.getString("username");
            String re_password = jsonObject.getString("password");
            int re_user_id = jsonObject.getInt("user_id");
    */

    GET和POST方式:

    GET:即在baseUrl后“ ?”  “ &”加一些参数即可

    POST方式:

    NameValuePair nameValuePair1 = new BasicNameValuePair("name",name); //name为读取的值  
    NameValuePair nameValuePair2 = new BasicNameValuePair("age",age); //age为读取的值  
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
    nameValuePairs.add(nameValuePair1);  
    nameValuePairs.add(nameValuePair2);  
    //生成Entity对象  
    HttpEntity requestHttpEntity = new UrlEncodedFormEntity(nameValuePairs);  
    HttpPost httpPost = new HttpPost(url); //这里的url是baseUrl 不用拼上?key=value...  
    httpPost.setEntity(requestHttpEntity );  
    //之后的操作一样  

     

  • 相关阅读:
    Mac 下安装Ant
    MAMP 10.10下启动报错解决方案
    [转]常用iOS图片处理方法
    Mac下Android SDK更新不了的解决办法
    细说23+1种设计模式
    mysql应该了解的知识点
    java快排思想
    简介一下 i++和++i&&i=i+i,i+=1;的区别
    对int类型的数据,如何让获取长度
    第一次写博客
  • 原文地址:https://www.cnblogs.com/mybkn/p/2508400.html
Copyright © 2011-2022 走看看