Android——(从Android studio端拿eclipse端的数据)

本文介绍了如何在Android应用中使用Jackson库将Java对象转换为Json,以及利用HttpClient进行HTTP请求与响应处理。在Android API 23之后,HttpClient被移除,但可以通过配置继续使用。文中详细讲解了HttpClient的使用步骤,包括创建HttpClient对象、发送GET/POST请求、处理响应状态码和内容。同时提到了解决HttpHostConnectException的方法,如添加INTERNET权限和确保正确使用IP地址。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. jackson
  1.1 Java->Json
    1.1.1 ObjectMap
    1.1.2 JsonGenerator
    1.1.3 @JsonIgnore
          hibernate中一对多、多对多双向关联会出现这种死循环

2. 简介
  2.1 HttpClient是Apache Jakarta Common下的子项目,用来提供高效的、最新的、
      功能丰富的支持HTTP协议的客户端编程工具包,并且它支持HTTP协议最新的版本和协议。
  2.2 Android已成功集成了HttpClient,这意味这开发人员可以直接在Android应用中使用
      HtppClient来提交请求、接收响应

      注:在API 23中,Google已经移除了移除了Apache HttpClient相关的类 ,
      在build.gradle中的android {}中加上
      useLibrary 'org.apache.http.legacy'
      详情见资料“”


      httpclient的作用是在后台模拟,浏览访问后台代码的操作
HttpClient的使用步骤
3. HttpClient的使用步骤
  3.1 创建HttpClient对象
      HttpClient httpClient = new DefaultHttpClient();

  3.2 创建HttpGet(或HttpPost)对象
      HttpGet HttpGet = new HttpGet("http://www.baidu.com");
      HttpPost httpPost = new HttpPost("http://www.baidu.com");

  3.3 添加参数(可选)
      setParams(HttpParams params)//HttpGet和HttpPost共有
      setEntity(HttpEntity entity)//HttpPost独有

      List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("age", "20"));
    params.add(new BasicNameValuePair("like", "aa"));
    params.add(new BasicNameValuePair("like", "bb"));
    params.add(new BasicNameValuePair("like", "cc"));
    params.add(new BasicNameValuePair("newsCategoryId", "1"));
    HttpEntity paramEntity = new UrlEncodedFormEntity(params,"UTF-8");
    httpPost.setEntity(paramEntity);
      
  3.4 发送GET(或POST)请求,并获得响应
      HttpResponse httpResponse = httpClient.execute(HttpUriRequest request);
 
      注1:HttpUriRequest为HttpGet和HttpPost的父类
      注2:需要添加允许网络访问权限,不然会报错“java.lang.SecurityException: Permission denied (missing INTERNET permission?)”
           <uses-permission android:name="android.permission.INTERNET" />

      注3:如果地址错误,或服务器未开户,HttpClient这SB会等待N久(>24小时)。
           所以请记得设置超时时间,所以请记得设置超时时间,所以请记得设置超时时间
           所以请记得设置超时时间,所以请记得设置超时时间,所以请记得设置超时时间
           所以请记得设置超时时间,所以请记得设置超时时间,所以请记得设置超时时间

           另外HttpClient版本不一样,代码也不一样。下面的4.0版本的写法
           httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 2000);// 连接时间
           httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 2000);// 数据传输时间

  3.5 处理响应
    3.5.1 响应状态码(200)
          httpResponse.getStatusLine().getStatusCode()

    3.5.2 响应头
          getAllHeaders()/getHeaders(String name)

    3.5.3 响应内容
          HttpEntity httpEntity = httpResponse.getEntity();//此对象包含服务器的响应内容
          String result = EntityUtils.toString(httpEntity);

案例二:使用HttpClientUtils与服务器通信
4. Android客户端与服务器通信
  4.1 JSON数据交换
      android系统内置了对象json的支持,另外json其实就以下几个对象的使用:
      JSONObject、JSONArray、JSONStringer、JSONException
  4.2 web service,底层使用xml交换数据 


5. 关于org.apache.http.conn.HttpHostConnectException: Connection to refused错误的解决办法
   增加网络访问权限:
   找到 AndroidManifest.xml 文件。在application标签后面加上 <uses-permission android:name="android.permission.INTERNET"/>
   检查ip地址:
   启动的android模拟器吧自己也当成127.0.0.1和localhost,如果使用了localhost或者127.0.0.1则会被拒绝访问,
   把ip地址改成实际地址,如:192.168.1.5

 

一个运行于 安卓系统的 小型web服务器,包括php/mysql 配置文件可以修改,另外可以用phpmyadmin 管理数据库。 让你的手机立刻变身小型服务器。 =============================== Turn your Android devices into a web and database server with Palapa Web Server, a suite of web developer. This application has been designed for low memory consumption and CPU usage, specially used for smartphone and tablet. Hey, it's free and you don't need a root access to run Palapa Web Server! # Requirements - Internal memory should not be less than 50MB! - ARM based processor - Minimum Android 2.2 Froyo # Features - Lighttpd 1.4.32 - PHP 5.5.1 - MySQL 5.1.69 - Msmtp 1.4.31 - phpMyAdmin 4.0.4.1 - Web Admin 1.0.1 # Default Document Root - Path : /sdcard/pws/www/ # Default URL - Address : http://127.0.0.1:8080/ # Web Admin Informations - Address : http://127.0.0.1:9999 - Username : admin - Password : admin # MySQL Informations - Host : localhost (127.0.0.1) - Port : 3306 - Username : root - Password : adminadmin # phpMyAdmin Informations - Address : http://127.0.0.1:9999/phpmyadmin - Username : root - Password : adminadmin # Problem? If something is not working properly, please try to restart your phone. # Known Issues As you know, compatibility issue is a big problem of Android phone, I can't test it in all phones. So if it can't work for your phone, just uninstall it, I'm sorry to waste your time. You also can send a mail to let me know your phone model, if I solve it, I will let you know.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值