AsyncHttpClient来完成网页源代码的显示功能,json数据在服务器端的读取还有安卓上的读取


一、使用AsyncHttpClient来完成网页源代码的显示功能:

首先。我们引入

步骤:

1.添加网络权限

2.判断网页地址是否为空

3.不为空的情况下创建客户端对象

4.处理get/post请求

5.如果成功的话,设置显示内容的值

a) 获取文件响应编码类型(保证不乱码)

i. 遍历头部信息取出contentType_value的值

ii. 定义服务器缺省编码方式

iii.  处理contentType_value来获取编码方式

1. contentType_value是否有“=

2. contentType_value是否为空

b) 根据服务端返回的编码给显示内容设置值

  1. package com.example.android_htmlcode;  
  2.   
  3. import java.io.UnsupportedEncodingException;  
  4.   
  5. import org.apache.http.Header;  
  6.   
  7. import android.app.Activity;  
  8. import android.os.Bundle;  
  9. import android.text.TextUtils;  
  10. import android.view.View;  
  11. import android.widget.EditText;  
  12. import android.widget.TextView;  
  13. import android.widget.Toast;  
  14.   
  15. import com.loopj.android.http.AsyncHttpClient;  
  16. import com.loopj.android.http.AsyncHttpResponseHandler;  
  17.   
  18. public class MainActivity extends Activity {  
  19.   
  20.     private EditText et_url;  
  21.     private TextView tv_url;  
  22.   
  23.     @Override  
  24.     protected void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.         // 设置显示视图  
  27.         setContentView(R.layout.activity_main);  
  28.         et_url = (EditText) findViewById(R.id.et_url);  
  29.         tv_url = (TextView) findViewById(R.id.tv_info);  
  30.     }  
  31.   
  32.     // android:onClick="sendHttpUil  
  33.     public void sendHttpUil(View v) {  
  34.         int id = v.getId();  
  35.         switch (id) {  
  36.         case R.id.btn_send:  
  37.   
  38.             String url = et_url.getText().toString();  
  39.             // 判断网页地址是否为空  
  40.             if (TextUtils.isEmpty(url)) {  
  41.                 Toast.makeText(this, "网页地址不能为空", 0).show();  
  42.             } else {  
  43.                 // 创建客户端对象  
  44.                 AsyncHttpClient client = new AsyncHttpClient();  
  45.   
  46.                 // 处理get/post请求  
  47.                 client.get(url, new AsyncHttpResponseHandler() {  
  48.   
  49.                     @Override  
  50.                     public void onSuccess(int statusCode, Header[] headers,  
  51.                             byte[] responseBody) {  
  52.                         super.onSuccess(statusCode, headers, responseBody);  
  53.                         // 获取文件响应类型  
  54.                         String contentType_value = null;  
  55.   
  56.                         // 遍历头部信息  
  57.                         for (Header header : headers) {  
  58.                             // 获取contentType_value的头部信息  
  59.                             if (header.getName().equals("Content-Type")) {  
  60.                                 // 获取他的value值  
  61.                                 contentType_value = header.getValue();  
  62.                             }  
  63.                         }  
  64.   
  65.                         // 定义服务器端缺省的编码方式  
  66.                         String default_charset = "UTF-8";  
  67.                         // 处理contentType_value来获取编码方式  
  68.                         // 判断是否为null  
  69.                         if (contentType_value != null) {  
  70.                             // 判断是否有=字符  
  71.                             if (contentType_value.contains("=")) {  
  72.                                 // 获取=字符位置  
  73.                                 int index = contentType_value.indexOf("=");  
  74.                                 // 从=所在位置的下一个字符开始截取,返回服务器端的编码  
  75.                                 default_charset = contentType_value.substring(  
  76.                                         index + 1, contentType_value.length());  
  77.                             } else {  
  78.                                 String result = new String(responseBody);  
  79.                                 default_charset = getCharSet(result);  
  80.                             }  
  81.                         } else {  
  82.                             String result = new String(responseBody);  
  83.                             default_charset = getCharSet(result);  
  84.                         }  
  85.                         Toast.makeText(MainActivity.this,  
  86.                                 "编码是:" + default_charset, 0).show();  
  87.   
  88.                         if (statusCode == 200) {  
  89.                             try {  
  90.                                 tv_url.setText(new String(responseBody,  
  91.                                         default_charset));  
  92.                             } catch (UnsupportedEncodingException e) {  
  93.                                 // TODO Auto-generated catch block  
  94.                                 e.printStackTrace();  
  95.                             }  
  96.                         }  
  97.                     }  
  98.   
  99.                     /** 
  100.                      * 获取网页源代码中默认的编码 
  101.                      *  
  102.                      * @param result 
  103.                      * @return 
  104.                      */  
  105.                     public String getCharSet(String result) {  
  106.                         String defaultCharset = null;  
  107.                         // <mate http-equiv="Content-Type"  
  108.                         // content="text/html; charset=GBK" /> //html4  
  109.                         // <mate charset="UTF-8">  
  110.                         if (result != null) {  
  111.                             if (result  
  112.                                     .contains("content=\"text/html; charset=GBK\"")) {  
  113.                                 defaultCharset = "GBK";  
  114.                             } else if (result  
  115.                                     .contains("content=\"text/html; charset=UTF-8\"")) {  
  116.                                 defaultCharset = "UTF-8";  
  117.                             } else if (result  
  118.                                     .contains("content=\"text/html; charset=GB2312\"")) {  
  119.                                 defaultCharset = "GB2312";  
  120.                             } else if (result.contains("charset=\"UTF-8\"")) {  
  121.                                 defaultCharset = "UTF-8";  
  122.                             } else if (result.contains("charset=\"UTF-8\"")) {  
  123.                                 defaultCharset = "GBK";  
  124.                             }  
  125.                         }  
  126.                         return defaultCharset;  
  127.                     }  
  128.   
  129.                 });  
  130.             }  
  131.   
  132.             break;  
  133.   
  134.         default:  
  135.             break;  
  136.         }  
  137.     }  
  138.   
  139. }  


 

二、通过开源框架获取JSON数据:

  1. package com.example.android_json;  
  2.   
  3. import org.apache.http.Header;  
  4. import org.json.JSONArray;  
  5. import org.json.JSONException;  
  6. import org.json.JSONObject;  
  7.   
  8. import com.loopj.android.http.AsyncHttpClient;  
  9. import com.loopj.android.http.JsonHttpResponseHandler;  
  10.   
  11. import android.os.Bundle;  
  12. import android.app.Activity;  
  13. import android.view.Menu;  
  14. import android.widget.Toast;  
  15.   
  16. public class MainActivity extends Activity {  
  17.   
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.activity_main);  
  22.         // http://172.16.237.200:8080/video/JsonServlet  
  23.         initData();  
  24.     }  
  25.   
  26.     private void initData() {  
  27.         // 创建客户端对象  
  28.         AsyncHttpClient client = new AsyncHttpClient();  
  29.         String url = "http://172.16.237.200:8080/video/JsonServlet";  
  30.         Toast.makeText(this, "发送请求到服务器", 0).show();  
  31.         client.get(url, new JsonHttpResponseHandler() {  
  32.               
  33.             //返回JSONObject对象|JSONOArray对象  
  34.             @Override  
  35.             public void onSuccess(int statusCode, Header[] headers,  
  36.                     JSONArray response) {  
  37.                 // TODO Auto-generated method stub  
  38.                 super.onSuccess(statusCode, headers, response);  
  39.   
  40.                 if (statusCode == 200) {  
  41.                     //遍历json数组  
  42.                     for (int i = 0; i < response.length(); i++) {  
  43.                         try {  
  44.                             // 获取具体的一个JSONObject对象  
  45.                             JSONObject obj = response.getJSONObject(i);  
  46.                             //JSONObject对象get(“属性名”),getString(“属性名”),getInt(“属性名”)等方法来获取指定属性名的值  
  47.                             System.out.println("序号" + obj.getString("id")  
  48.                                     + "--------姓名:" + obj.getString("name")  
  49.                                     + "--------密码:" + obj.getString("pass")  
  50.                             + "--------其他:" + obj.getString("pass"));  
  51.                         } catch (JSONException e) {  
  52.                             // TODO Auto-generated catch block  
  53.                             e.printStackTrace();  
  54.                         }  
  55.                     }  
  56.                 }  
  57.             }  
  58.         });  
  59.     }  
  60.   
  61. }  

常见错误分析:当遍历json数组时,若没有对于json数据就会报错,例如:(部分代码整理源于赵雅智女士的)

AsyncHttpClient来完成网页源代码的显示功能,json数据在服务器端的读取还有安卓上的读取,布布扣,bubuko.com

AsyncHttpClient来完成网页源代码的显示功能,json数据在服务器端的读取还有安卓上的读取

上一篇:Android4.4 之Bluetooth整理


下一篇:android开源框架android-async-http使用