`
liangoogle
  • 浏览: 273303 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

apache httpget 网络连接 读取数据

阅读更多

 

public class Test extends Activity implements OnClickListener {

    public Context context;

    private TextView textView1;

    DefaultHttpClient  httpClient ;

    public static String URL ="http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl";

    private static final int TIMEOUT = 60;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        init();

        try {

            

            

            HttpParams paramsw = createHttpParams();

            httpClient = new DefaultHttpClient(paramsw);

            HttpGet httpGet=new HttpGet("http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl");

                try {

                    HttpResponse httpResponse = httpClient.execute(httpGet); //连接服务器

                int httpCode = httpResponse.getStatusLine().getStatusCode();

                if (httpCode == HttpURLConnection.HTTP_OK) {

                    Header[] headers=httpResponse.getAllHeaders();

                    HttpEntity entity=httpResponse.getEntity();

                    Header header=httpResponse.getFirstHeader("content-type");

                    

                    //下面是从流中把字符串读出来了

                    InputStream inputStream=entity.getContent();

                    InputStreamReader inputStreamReader=new InputStreamReader(inputStream);

                    BufferedReader reader = new BufferedReader(inputStreamReader);//读字符串用的。

                    String inputLine = null;

                    String result = "";

                    // 使用循环来读取获得的数据,把数据都村到result中了

                    while (((inputLine = reader.readLine()) != null)) {

                        // 我们在每一行后面加上一个"\n"来换行

                        result += inputLine + "\n";

                    }

                    reader.close();//关闭输入流

                }else {

                    textView1.setText("Error Response"+httpResponse.getStatusLine().toString());

                }

            } catch (UnsupportedEncodingException e) {

                textView1.setText("网络连接错误UnsupportedEncodingException");

            } catch (ClientProtocolException e) {

                textView1.setText("网络连接错误ClientProtocolException");

            } catch (IOException e) {

                textView1.setText("网络连接错误IOException");

            }

        } finally {


            if (httpClient != null) {

                httpClient.getConnectionManager().shutdown();//最后关掉链接。

                httpClient = null;

            }

        }

    }

相关链接:http://www.eoeandroid.com/thread-13873-1-1.html

 

分享到:
评论

相关推荐

    android 常用工具类

    Http网络工具类,主要包括httpGet、httpPost以及http参数相关方法,以httpGet为例: static HttpResponse httpGet(HttpRequest request) static HttpResponse httpGet(java.lang.String httpUrl) static String ...

    PHP函数索引

    apache_get_modules - Get 一个目录载入 apache 模块。 apache_getenv - Get接入apache指定的环境变量。 apache_lookup_uri - 对指定的URI执行部分请求并返回所有有关信息。 apache_note - 取得或设置apache请求记录...

    httpClient

    // 读取为 InputStream,在网页内容数据量大时候推荐使用 // InputStream response = getMethod.getResponseBodyAsStream(); } catch (HttpException e) { // 发生致命的异常,可能是协议不对或者返回的内容有...

    PHP网络编程技术与实践 源码

    3.4.4 获取HTTP GET信息:$_GET 3.4.5 获取HTTP POST信息:$_POST 3.4.6 HTTP文件上传信息:$_FILES 3.4.7 REQUEST变量:$_REQUEST 3.4.8 系统Session信息:$_SESSION 3.4.9 GLOBALS变量:$GLOBALS 3.5 本章小结 第4...

    达内客户端+聊天室源码

    import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.params.ClientPNames; import org.apache.http.impl.client.CloseableHttpClient...

    Android网络请求之OkHttp

    OkHttp是一款优秀的HTTP框架,它支持get请求和post请求,支持基于Http的文件上传和下载,支持加载图片,支持下载文件透明的GZIP压缩,支持响应缓存避免重复的网络请求,支持使用连接池来降低响应延迟问题 Get请求 ...

    php网络开发完全手册

    1.5 几种综合网络服务器系统的安装 14 1.5.1 XAMPP 14 1.5.2 WAMP 16 1.5.3 Appserv 17 1.5.4 EasyPHP 18 1.5.5 VertrigoServ 19 1.6 几种开发工具的介绍 20 1.6.1 Vi及Vim 20 1.6.2 Eclipse+PHPEclipse插件 21 ...

    HttpClient以及获取页面内容应用

    HttpGet httpGet = new HttpGet("http://www.baidu.com/"); CloseableHttpResponse response = httpClient.execute(httpGet); InputStream is = null; Scanner sc = null; Writer os = null; if ...

    ASP EXCEL导入SQL

    AJAX技术充分利用了HTTP来获取网络资源并且实现了HTTP没有的对于异步数据进行传输的功能。AJAX技术还使得软件更好地实现分布性功能,在一个企业内只要一个人下载了AJAX引擎,其它企业内部的人员,就可以共享该资源了...

    JAVA上百实例源码以及开源项目源代码

     Java实现的FTP连接与数据浏览程序,实现实例化可操作的窗口。  部分源代码摘录:  ftpClient = new FtpClient(); //实例化FtpClient对象  String serverAddr=jtfServer.getText(); //得到服务器地址  ...

    JAVA上百实例源码以及开源项目

    百度云盘分享 ... Java实现的FTP连接与数据浏览程序,实现实例化可操作的窗口。  部分源代码摘录:  ftpClient = new FtpClient(); //实例化FtpClient对象  String serverAddr=jtfServer.getText();...

    Android静默安装常用工具类

    Http网络工具类,主要包括httpGet、httpPost以及http参数相关方法,以httpGet为例: static HttpResponse httpGet(HttpRequest request) static HttpResponse httpGet(java.lang.String httpUrl) static String ...

    Interface的测试代码,请光临

    // //读取内容 // byte[] responseBody = getMethod.getResponseBody(); // //处理内容 // System.out.println(new String(responseBody)); InputStream stream = getMethod.getResponseBodyAsStream...

    用HttpClient来模拟浏览器GET POST

    http://www.xd-tech.com.cn/blog/article.asp?id=34<br><br>一般的情况下我们都是使用IE或者Navigator浏览器来访问一个WEB服务器,用来浏览页面查看信息或者提交一些数据等等。所访问的这些页面有的仅仅是一些普通的...

    基于J2EE框架的个人博客系统项目毕业设计论文(源码和论文)

    随着博客人数的增加, Blog 作为一种新的生活方式、新的工作方式、新的学习方式已经被越来越多的人所接受,并且在改变传统的网络和社会结构:网络信息不再是虚假不可验证的,交流和沟通更有明确的选择和方向性,单一...

    XML轻松学习手册--XML肯定是未来的发展趋势,不论是网页设计师还是网络程序员,都应该及时学习和了解

    我的理解是它满足了网络共享和数据交互,使用DTD最大的好处在于DTD文件的共享。(就是上文DTD说明语句中的PUBLIC属性)。比如,两个相同行业不同地区的人使用同一个DTD文件来作为文档创建规范,那么他们的数据就很容易...

    ssh(structs,spring,hibernate)框架中的上传下载

    对于那些仅封装了Connection而未包括Statement的简单数据连接池,SimpleNativeJdbcExtractor是效率最高的抽取器实现类,但具体到apache的BasicDataSource连接池,它封装了所有JDBC的对象,这时就需要使用...

    JS API 接口接收与返回

    //启动压缩输出虽然可以让传输的数据量减少,但是它会占用更多的内存和 CPU,因此它默认是关闭的。 $server->start(); //开启服务 ?> -------------------------------------------------- -----------------...

Global site tag (gtag.js) - Google Analytics