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) 根据服务端返回的编码给显示内容设置值
- package com.example.android_htmlcode;
- import java.io.UnsupportedEncodingException;
- import org.apache.http.Header;
- import android.app.Activity;
- import android.os.Bundle;
- import android.text.TextUtils;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.loopj.android.http.AsyncHttpClient;
- import com.loopj.android.http.AsyncHttpResponseHandler;
- public class MainActivity extends Activity {
- private EditText et_url;
- private TextView tv_url;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // 设置显示视图
- setContentView(R.layout.activity_main);
- et_url = (EditText) findViewById(R.id.et_url);
- tv_url = (TextView) findViewById(R.id.tv_info);
- }
- // android:onClick="sendHttpUil
- public void sendHttpUil(View v) {
- int id = v.getId();
- switch (id) {
- case R.id.btn_send:
- String url = et_url.getText().toString();
- // 判断网页地址是否为空
- if (TextUtils.isEmpty(url)) {
- Toast.makeText(this, "网页地址不能为空", 0).show();
- } else {
- // 创建客户端对象
- AsyncHttpClient client = new AsyncHttpClient();
- // 处理get/post请求
- client.get(url, new AsyncHttpResponseHandler() {
- @Override
- public void onSuccess(int statusCode, Header[] headers,
- byte[] responseBody) {
- super.onSuccess(statusCode, headers, responseBody);
- // 获取文件响应类型
- String contentType_value = null;
- // 遍历头部信息
- for (Header header : headers) {
- // 获取contentType_value的头部信息
- if (header.getName().equals("Content-Type")) {
- // 获取他的value值
- contentType_value = header.getValue();
- }
- }
- // 定义服务器端缺省的编码方式
- String default_charset = "UTF-8";
- // 处理contentType_value来获取编码方式
- // 判断是否为null
- if (contentType_value != null) {
- // 判断是否有=字符
- if (contentType_value.contains("=")) {
- // 获取=字符位置
- int index = contentType_value.indexOf("=");
- // 从=所在位置的下一个字符开始截取,返回服务器端的编码
- default_charset = contentType_value.substring(
- index + 1, contentType_value.length());
- } else {
- String result = new String(responseBody);
- default_charset = getCharSet(result);
- }
- } else {
- String result = new String(responseBody);
- default_charset = getCharSet(result);
- }
- Toast.makeText(MainActivity.this,
- "编码是:" + default_charset, 0).show();
- if (statusCode == 200) {
- try {
- tv_url.setText(new String(responseBody,
- default_charset));
- } catch (UnsupportedEncodingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- /**
- * 获取网页源代码中默认的编码
- *
- * @param result
- * @return
- */
- public String getCharSet(String result) {
- String defaultCharset = null;
- // <mate http-equiv="Content-Type"
- // content="text/html; charset=GBK" /> //html4
- // <mate charset="UTF-8">
- if (result != null) {
- if (result
- .contains("content=\"text/html; charset=GBK\"")) {
- defaultCharset = "GBK";
- } else if (result
- .contains("content=\"text/html; charset=UTF-8\"")) {
- defaultCharset = "UTF-8";
- } else if (result
- .contains("content=\"text/html; charset=GB2312\"")) {
- defaultCharset = "GB2312";
- } else if (result.contains("charset=\"UTF-8\"")) {
- defaultCharset = "UTF-8";
- } else if (result.contains("charset=\"UTF-8\"")) {
- defaultCharset = "GBK";
- }
- }
- return defaultCharset;
- }
- });
- }
- break;
- default:
- break;
- }
- }
- }
二、通过开源框架获取JSON数据:
- package com.example.android_json;
- import org.apache.http.Header;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import com.loopj.android.http.AsyncHttpClient;
- import com.loopj.android.http.JsonHttpResponseHandler;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- // http://172.16.237.200:8080/video/JsonServlet
- initData();
- }
- private void initData() {
- // 创建客户端对象
- AsyncHttpClient client = new AsyncHttpClient();
- String url = "http://172.16.237.200:8080/video/JsonServlet";
- Toast.makeText(this, "发送请求到服务器", 0).show();
- client.get(url, new JsonHttpResponseHandler() {
- //返回JSONObject对象|JSONOArray对象
- @Override
- public void onSuccess(int statusCode, Header[] headers,
- JSONArray response) {
- // TODO Auto-generated method stub
- super.onSuccess(statusCode, headers, response);
- if (statusCode == 200) {
- //遍历json数组
- for (int i = 0; i < response.length(); i++) {
- try {
- // 获取具体的一个JSONObject对象
- JSONObject obj = response.getJSONObject(i);
- //JSONObject对象get(“属性名”),getString(“属性名”),getInt(“属性名”)等方法来获取指定属性名的值
- System.out.println("序号" + obj.getString("id")
- + "--------姓名:" + obj.getString("name")
- + "--------密码:" + obj.getString("pass")
- + "--------其他:" + obj.getString("pass"));
- } catch (JSONException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- });
- }
- }
常见错误分析:当遍历json数组时,若没有对于json数据就会报错,例如:(部分代码整理源于赵雅智女士的)