本文出自:http://blog.csdn.net/zhongnan09/article/details/6565384
在AndroidSDK中,有很多包都支持网络编程,下面列举了Android中集成的网络编程相关的包:
包
|
描述
|
java.net
|
提供与联网有关的类,包括流和数据包(datagram)sockets、Internet 协议和常见 HTTP 处理。该包是一个多功能网络资源。有经验的 Java 开发人员可以立即使用这个熟悉的包创建应用程序。
|
java.io
|
虽然没有提供显式的联网功能,但是仍然非常重要。该包中的类由其他 Java 包中提供的 socket 和连接使用。它们还用于与本地文件(在与网络进行交互时会经常出现)的交互。
|
java.nio
|
包含表示特定数据类型的缓冲区的类。适合用于两个基于 Java 语言的端点之间的通信。
|
org.apache.*
|
表示许多为 HTTP 通信提供精确控制和功能的包。可以将 Apache 视为流行的开源 Web 服务器。
|
android.net
|
除核心 java.net.* 类以外,包含额外的网络访问 socket。该包包括 URI 类,后者频繁用于 Android 应用程序开发,而不仅仅是传统的联网方面。
|
android.net.http
|
包含处理 SSL 证书的类。
|
android.net.wifi
|
包含在 Android 平台上管理有关 WiFi(802.11 无线 Ethernet)所有方面的类。并不是所有设备都配备了 WiFi 功能,特别是 Android 在 Motorola 和 LG 等手机制造商的 “翻盖手机” 领域获得了成功。
|
android.telephony.gsm
|
包含用于管理和发送 SMS(文本)消息的类。一段时间后,可能会引入额外的包来来为非 GSM 网络提供类似的功能,比如 CDMA 或 android.telephony.cdma 等网络。
|
已经集成了Apache的HttpClient模块,在这一节中,我们练习如何应用HttpClient创建连接并从网络上获取数据。
下面是Demo:
首先新建Android工程,并建立启动Activity:
MainActivity.java:
package com.xzq.httpclient;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity
{
private Button mButton1, mButton2;
private TextView mTextView1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton1 = (Button) findViewById(R.id.myButton1);
mButton2 = (Button) findViewById(R.id.myButton2);
mTextView1 = (TextView) findViewById(R.id.myTextView1);
mButton1.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
String uriAPI = "https://passport.sohu.com/web/dispatchAction.action";
//使用post方式
HttpPost httpRequest = new HttpPost(uriAPI);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("str", "I am Post String"));
try
{
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse httpResponse = new DefaultHttpClient()
.execute(httpRequest);
if (httpResponse.getStatusLine().getStatusCode() == 200)
{
String strResult = EntityUtils.toString(httpResponse.getEntity());
mTextView1.setText(strResult);
} else
{
mTextView1.setText("响应错误: "
+ httpResponse.getStatusLine().toString());
}
} catch (ClientProtocolException e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
} catch (IOException e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
} catch (Exception e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
}
});
mButton2.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
String uriAPI = "http://www.baidu.com";
//使用get方式
HttpGet httpRequest = new HttpGet(uriAPI);
try
{
HttpResponse httpResponse = new DefaultHttpClient()
.execute(httpRequest);
if (httpResponse.getStatusLine().getStatusCode() == 200)
{
String strResult = EntityUtils.toString(httpResponse.getEntity());
mTextView1.setText(strResult);
} else
{
mTextView1.setText("错误消息: "
+ httpResponse.getStatusLine().toString());
}
} catch (ClientProtocolException e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
} catch (IOException e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
} catch (Exception e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
}
});
}
}
Post方式的效果:
get方式效果如下:
要注意在main.xml文件中给TextView加上ScrollView标签,否则,当页面数据比较大时,不能出现滚动条,影响使用。另外,同样要在AndroidManifest.xml文件中添加访问网络的权限。
分享到:
相关推荐
该Demo主要介绍使用HttpClient获取网络数据,然后经过JSON后显示出来。另外本文章用到了常用的异步加载技术,AsyncTask类。
Httpclient上传文件,非常有用的jar包 apache-mime4j-0.6.jar httpmime-4.0.jar
Android HttpClient基本使用方法 测试过能用。
struts2 android httpclient 上传文件 客服端 服务器端 亲测通过
Android利用HttpClient实现访问web服务器源码 剥离出HttpClient代码,用来做网页爬虫亦可
这是一个用Httpclient设置代理下载文件的例子
Android 使用HttpClient代理
利用 android httpclient 编写http协议的例子代码,可以参考添加post,get ,put delete 等功能。工程师文件可以直接编译运行。
android9.0不再支持useLibrary 'org.apache.http.legacy'引入httpclient,需要引入jar
android应用开发 httpClient所需jar包 和调用webservice 工具ksoap2 jar 包
android的Http上载文件 可以实现多文件上载,结合android自身白日提供的强大的HttpClient操作。
在黑马程序员教材《Android移动应用开发基础案例教程》中,有一个仿美团项目案例,需要使用OkHttpClient访问网络,教材中说需要使用第三方模拟器,否则访问不到数据。实际上,在清单文件AndroidManifest.xml进行一定...
android HttpClient 获取网络图片 实例
Android使用HttpClient上传文件到服务器完整实例,包括要用到的jar都下libs下面,服务器的源码也有
下面给大家展示了AndroidHttpClient结构: public final class AndroidHttpClient extends Object implements HttpClient 前言: 这类其实是Google对阿帕奇的HttpClient的一个封装,一些默认属性有android做了一些...
是android的jar中httpclient的源码,是org.apache.http包的源码。其中包含org.apache.http.HttpResponse类。 不是官网上下载的,官网上的源码里面没有org.apache.http.HttpResponse这个类。
Android-异步HttpClient框架,采用两种提交方式 Android-异步HttpClient框架(get和post) Android-异步HttpClient框架 csdn博客地址:http://blog.csdn.net/u014657752
今天想把一个用使用了HttpClient的自动签到小程序移植到Android上,还好Android的SDK自带了HttpClient的包.当然也可以继续使用DefaultHttpClient,但用为Android定制的AndroidHttpClient自然更好
Android Studio使用HttpClient请求数据(get请求)不包括post请求
本实例描述了Android平台下使用Apache开源项目的HttpClient访问网络的示例。分别使用HttpClient下的两个子实现类,DefaultHttpClient和AndroidHttpClient分别进行演示。