一、使用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);
- }
-
-
- 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();
-
-
- 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) {
-
- if (header.getName().equals("Content-Type")) {
-
- contentType_value = header.getValue();
- }
- }
-
-
- String default_charset = "UTF-8";
-
-
- 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) {
-
- e.printStackTrace();
- }
- }
- }
-
-
- public String getCharSet(String result) {
- String defaultCharset = null;
-
-
-
- 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);
-
- 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() {
-
-
- @Override
- public void onSuccess(int statusCode, Header[] headers,
- JSONArray response) {
-
- super.onSuccess(statusCode, headers, response);
-
- if (statusCode == 200) {
-
- for (int i = 0; i < response.length(); i++) {
- try {
-
- JSONObject obj = response.getJSONObject(i);
-
- System.out.println("序号" + obj.getString("id")
- + "--------姓名:" + obj.getString("name")
- + "--------密码:" + obj.getString("pass")
- + "--------其他:" + obj.getString("pass"));
- } catch (JSONException e) {
-
- e.printStackTrace();
- }
- }
- }
- }
- });
- }
-
- }
常见错误分析:当遍历json数组时,若没有对于json数据就会报错,例如:(部分代码整理源于赵雅智女士的)