Android中使用HttpClient实现HTTP通信效果
生活随笔
收集整理的這篇文章主要介紹了
Android中使用HttpClient实现HTTP通信效果
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
HTTP通信,這一案例在操作的時(shí)候遇到N多種種問題,是前面看過幾個(gè)實(shí)例里面最麻煩的一個(gè),因?yàn)闆]有系統(tǒng)的接觸過JAVA,所以出了很多錯(cuò)誤,也無從下手解決,這里經(jīng)過對錯(cuò)誤的檢索實(shí)現(xiàn)了HTTP通信,以做記錄。 實(shí)現(xiàn) HTTP 通信有兩種方式,一種是 ?httpurlconnection ,另一種是 ?HttpClient ,出于開發(fā)習(xí)慣,這里選擇了 HttpClient 做了一次實(shí)現(xiàn) 看實(shí)例 ?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 實(shí)現(xiàn)了簡單的封裝,以后使用只需調(diào)用對應(yīng)的函數(shù)就可以了,下面看一個(gè)調(diào)用 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();
}}
以上代碼實(shí)現(xiàn)了對 ApacheHttpClient.java的調(diào)用,實(shí)現(xiàn)效果是會在應(yīng)用界面上顯示服務(wù)端內(nèi)容。 這里需要說明的是,在調(diào)試過程中我遇到的問題 1、?android.os.NetworkOnMainThreadException 異常 這里說明的是,不能在主線程中訪問網(wǎng)絡(luò) 2、Only the original thread that created a view hierarchy can touch its views 這里說明的是Android的相關(guān)View和控件不是線程安全的,我們必須做獨(dú)立的處理,所以這里通過一個(gè)Handler對象可以很好的傳遞Runnable或Message
總結(jié)
以上是生活随笔為你收集整理的Android中使用HttpClient实现HTTP通信效果的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android 阅读器架构图,网上收集,
- 下一篇: Android使用GestureDete