转载,原文出处: http://blog.zhourunsheng.com/2012/03/android-%E5%BC%80%E5%8F%91%E4%B9%8B-httpclient-class-can-not-find-%E4%B9%8B%E8%A7%A3%E5%86%B3%E5%8A%9E%E6%B3%95/
相信大家在开发 Android 的过程中遇到过这么一种情况,那就是 “Could not find class 'org.apache.commons.httpclient.HttpClient'”。
尤其是在 eclipse 的插件ADT升级之后,很容易出现该问题,昨天Google放出了ADT的升级包,然后我也就升级了一下开发环境,没想到前天还运行好好的程序,今天突然就不会工作了,检查log发现,HttpClient无法找到,但是在普通的Java运行环境下就可以正常运行。
因为Apache的HttpClient开发包,Google自己也定制了一份,已经内置到开发环境中了,所以我们如果使用纯粹的Apache原生态的HttpClient开发包就可能出现冲突的问题,每当Google升级ADT之后,说不定程序就Over了。
下面是自己写的基于Google的一个简单的代码
package mars.httpclienttest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
//
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.util.Log;
import java.net.URL;
//
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class HttpActivity extends Activity{
//
private String strURL = "xxxxxxxx";
//
private Button loginButton = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// TODO Auto-generated method stub
HttpGet objGet = new HttpGet(strURL);
HttpClient client = new DefaultHttpClient();
try
{
HttpResponse response = client.execute(objGet);
System.out.println(EntityUtils.toString(response.getEntity()));
} catch (Exception e)
{
Log.e("httpGet", e.getMessage());
}
}
}