zoukankan      html  css  js  c++  java
  • Android中使用HttpClient实现HTTP通信效果

    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
  • 相关阅读:
    UVA1585
    暑期第二场-1
    UVA11582
    UVA10006
    HDU1005
    HDU2035
    POJ:2492-Bug's Life(二分图的判定)
    Codeforces:68A-Irrational problem(暴力大法好)
    Codeforces Round #456 (Div. 2) B. New Year's Eve
    Codeforces Round #456 (Div. 2) A. Tricky Alchemy
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/6978203.html
Copyright © 2011-2022 走看看