HTTP通信,这一案例在操作的时候遇到N多种种问题,是前面看过几个实例里面最麻烦的一个。由于没有系统的接触过JAVA,所以出了非常多错误,也无从下手解决,这里经过对错误的检索实现了HTTP通信,以做记录。

实现 HTTP 通信有两种方式,一种是  httpurlconnection ,还有一种是  HttpClient ,出于开发习惯。这里选择了 HttpClient 做了一次实现

看实例  ApacheHttpClient.java

package com.example.httpclient;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream; import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient; import android.util.Log; public class ApacheHttpClient {
private static final String TAG = "Error"; public String httpGet(String url) {
String result = null;
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = null;
try {
httpResponse = httpClient.execute(httpGet);
int httpStatus = httpResponse.getStatusLine().getStatusCode();
if (httpStatus == HttpStatus.SC_OK) {
InputStream in = httpResponse.getEntity().getContent();
try {
result = readString(in);
} catch (Exception e) {
Log.i(TAG, "Exception");
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
result = "badly";
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i(TAG, "ClientProtocolException");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i(TAG, "ClientProtocolException");
}
return result;
} protected String readString(InputStream in) throws Exception {
byte[] data = new byte[1024];
int length = 0;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
while ((length = in.read(data)) != -1) {
bout.write(data, 0, length);
}
return new String(bout.toByteArray(), "GBK");
}
}

以上代码将 HttpClient 实现了简单的封装。以后使用仅仅需调用相应的函数就能够了,以下看一个调用 MainActivity.java

package com.example.httpclient;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView; public class MainActivity extends Activity { private Handler handler = null;
private String result = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); handler = new Handler();
new Thread() {
public void run() {
ApacheHttpClient aHttpClient = new ApacheHttpClient();
result = aHttpClient.httpGet("http://192.168.1.100");
handler.post(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
TextView tv = (TextView)findViewById(R.id.textView1);
tv.setText(result);
}});
}
}.start();
} }

以上代码实现了对 ApacheHttpClient.java的调用,实现效果是会在应用界面上显示服务端内容。

这里须要说明的是,在调试过程中我遇到的问题

1、 android.os.NetworkOnMainThreadException 异常

这里说明的是,不能在主线程中訪问网络

2、Only the original thread that created a view hierarchy can touch its views

这里说明的是Android的相关View和控件不是线程安全的。我们必须做独立的处理,所以这里通过一个Handler对象能够非常好的传递Runnable或Message

Android中使用HttpClient实现HTTP通信效果的更多相关文章

  1. 网络相关系列之中的一个:Android中使用HttpClient发送HTTP请求

    一.HTTP协议初探: HTTP(Hypertext Transfer Protocol)中文 "超文本传输协议",是一种为分布式,合作式,多媒体信息系统服务,面向应用层的协议,是 ...

  2. android中四大组件之间相互通信

    好久没有写有关android有关的博客了,今天主要来谈一谈android中四大组件.首先,接触android的人,都应该知道android中有四大组件,activity,service,broadca ...

  3. Mono For Android中简单实现按钮的动画效果

    Android中动画的分Tween Animation和Frame Animation,本节主要讲Tween Animation的实现. 一般是通过XML文件来定义动画的,具体如下: 1.在项目res ...

  4. Android中实现圆角矩形及半透明效果。

    注:本文由Colin撰写,版权所有!转载请注明原文地址,谢谢合作! 在做Android开发时,我们为了美观,有时候需要使用圆角矩形,或半透明之类的效果,在网页设计中很容易实现.但在Android开发中 ...

  5. android中利用实现二级联动的效果

    按照惯例,首先上一张效果图. 本篇文章实现的效果就是如图中所圈的那样,实现类似于HTML中的二级联动的效果. 对于第一个选项我们读取的是本地xml文件来填充数据的, 对于第二个选项我们读取的是通过中央 ...

  6. android中listview的item滑动删除效果(已解决listview点击问题)

    领导看到iphone上tableview有个滑动删除的效果,要求在android上也实现,搜了下资料,实现起来比较简单,可弄到后面,居然不能点击了,把一篇文章中的代码修改了一下,捣鼓了一番,搞定,下面 ...

  7. Android中利用httpclient进行网络通信的方法(以用户登录为例说明)

    http://www.android100.org/html/201406/09/22915.html 1.服务器端 服务器端和android没有太大关系,对J2EE比较熟悉的话写起来应该很容易,这里 ...

  8. android 中的 Handler 线程间通信

    一. 在MainActivity中为什么只是类似的写一行如下代码就可以使用handler了呢? Handler handler = new Handler() { @Override public v ...

  9. android中fragment与activity之间通信原理以及例子

    参考文章 http://blog.csdn.net/guozh/article/details/25327685#comments Activity和fragment通信方式一般有3种方法 1.在fr ...

随机推荐

  1. 360动态加载的Android插件框架

    github地址:https://github.com/Qihoo360/DroidPlugin DroidPlugin 是360手机助手在Android系统上实现了一种新的插件机制:它可以在无需安装 ...

  2. 智课雅思词汇---四、clos和cap和ced是什么意思

    智课雅思词汇---四.clos和cap和ced是什么意思 一.总结 一句话总结: cap/capt/cip/cep/ceiv:to take,seize(拿,抓住) cede:to go,yield( ...

  3. centos7 nginx搭建及其反向代理

    摘要:nginx反向代理的原理:外部通过ip加端口访问nginx,nginx接收到外部请求,通过ip解析访问内部服务器,内部服务器再将数据传回Nginx服务器,而Nginx再把数据传回给外部客户机. ...

  4. Coderfroces 862 C. Mahmoud and Ehab and the xor

    C. Mahmoud and Ehab and the xor Mahmoud and Ehab are on the third stage of their adventures now. As ...

  5. U-BOOT概述及源码分析(一)

    嵌入式Linux系统从软件角度通常可以分为以下4个层次: 引导加载程序 | Linux内核 | 文件系统 | 用户应用程序 嵌入式Linux系统中典型分区结构: 正常启动过程中,Bootloader首 ...

  6. django项目所遇问题总结

    2. 关于设置static静态文件,样式失效问题 原因: 可能开启多个端口号,页面显示访问的不是已经设置了static的模板,所以,样式没有显示 3. models模型中gender字段的选择设置 c ...

  7. mkdi---创建目录。

    mkdir命令用来创建目录.该命令创建由dirname命名的目录.如果在目录名的前面没有加任何路径名,则在当前目录下创建由dirname指定的目录:如果给出了一个已经存在的路径,将会在该目录下创建一个 ...

  8. UVALive 6867 Plane Ticket Pricing

    Plane Ticket Pricing   Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu ...

  9. 安装Apache PHP MySQL PHPMyAdmin

    视频教程:https://www.youtube.com/watch?v=FJC2iGt_2bc,Youtube看不了的FQ吧-3- 本人参考这篇文章:http://blog.csdn.net/kno ...

  10. TCP的可靠性 窗口滑动 拥塞控制

    看这篇文章: http://www.cnblogs.com/woaiyy/p/3554182.html 窗口滑动,如下图: 流量控制 流量控制方面主要有两个要点需要掌握.一是TCP利用滑动窗口实现流量 ...