Android http
来源:互联网 发布:mac程序卸载后图标还在 编辑:程序博客网 时间:2024/05/16 08:27
- ImageView iv = new ImageView(context);
- iv.setId(12351);
- String imageUrl = "http://i.pbase.com/o6/92/229792/1/80199697.uAs58yHk.50pxCross_of_the_Knights_Templar_svg.png"; //标准HTTP地址即可
- try {
- URL myurl = new URL(imageUrl);
- HttpURLConnection conn = (HttpURLConnection) myurl.openConnection();
- conn.setDoInput(true);
- conn.connect();
- InputStream is = conn.getInputStream();
- Bitmap bitmap = BitmapFactory.decodeStream(is);
- is.close();
- iv.setImageBitmap(bitmap);
- } catch (Exception e) {
- // TODO: handle exception
- }
- layout.addView(iv);
在Manifest.xml中加入uses-permission配置,允许进行网络访问
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="org.studio.crusoe.sample.android" android:versionCode="1"
- android:versionName="1.0">
- <uses-permission android:name="android.permission.INTERNET" />
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".ActivityMain" android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- <uses-sdk android:minSdkVersion="2" />
- </manifest>
方法二:使用Apache API:
1、使用Map来存储参数
Map<String, String> map = new HashMap<String, String>();
map.put(“name”, “wusheng”);
map.put(“password”, “pwd”);
2、使用DefaultHttpClient创建HttpClient实例
DefaultHttpClient httpClient = new DefaultHttpClient();
3、构建HttpPost
HttpPost post = new HttpPost(“http://wu-sheng.iteye.com”);
4、将由Map存储的参数转化为键值参数
List<BasicNameValuePair> postData = new ArrayList<BasicNameValuePair>();
for (Map.Entry<String, String> entry : map.entrySet()) {
postData.add(new BasicNameValuePair(entry.getKey(),
entry.getValue()));
}
5、使用编码构建Post实体
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(
postData, HTTP.UTF_8);
6、设置Post实体
post.setEntity(entity);
7、执行Post方法
HttpResponse response = httpClient.execute(post);
8、获取返回实体
HttpEntity httpEntity = response.getEntity();
9、将H中返回实体转化为输入流
InputStream is = httpEntity.getContent();
10、读取输入流,即返回文本内容
StringBuffer sb = new StringBuffer();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = “”;
while((line=br.readLine())!=null){
sb.append(line);
}
例如:Android 通过Http访问Web端的Servlet
- //Http工具类
- import org.apache.http.HttpResponse;
- import org.apache.http.HttpStatus;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.params.BasicHttpParams;
- import org.apache.http.params.HttpConnectionParams;
- import org.apache.http.params.HttpParams;
- import org.apache.http.util.EntityUtils;
- public class HttpUtil {
- public static String getHttpJSON(String url) {
- HttpGet httpRequest = new HttpGet(url);
- try {
- HttpClient httpclient = new DefaultHttpClient();
- HttpResponse httpResponse = httpclient.execute(httpRequest);
- if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
- String jsonStr = EntityUtils.toString(httpResponse.getEntity(),
- "UTF-8");
- return jsonStr;
- }
- } catch (Exception e) {
- e.printStackTrace();
- System.out.println("==============e.printStackTrace() : "
- + e.getMessage());
- }
- return null;
- }
- public static int getHttpStatus() {
- int status = 0;
- HttpGet httpRequest = new HttpGet(
- "http://192.168.0.214/vote/AndroidConnServlet");
- try {
- HttpParams httpParameters = new BasicHttpParams();
- HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
- HttpConnectionParams.setSoTimeout(httpParameters, 5000);
- HttpConnectionParams.setTcpNoDelay(httpParameters, true);
- HttpClient httpclient = new DefaultHttpClient(httpParameters);
- HttpResponse httpResponse = httpclient.execute(httpRequest);
- status = httpResponse.getStatusLine().getStatusCode();
- } catch (Exception e) {
- e.printStackTrace();
- System.out
- .println("==============connection wifi fail,e.printStackTrace() : "
- + e.getMessage());
- }
- return status;
- }
- }
- //调用方法
- public void ensureVote() {
- String URL = "http://192.168.0.214/vote/VoteServlet";
- codeText = codeEdit.getText().toString();
- if (codeText == null || codeText.length() == 0) {
- Toast.makeText(VoteActivity.this, "投票失败,请输入投票码.",
- Toast.LENGTH_LONG).show();
- return;
- }
- URL = URL + "?project=" + radioVoteText + "&voteCode=" + codeText
- + "&source=Android";
- String httpStatus = "0";
- httpStatus = HttpUtil.getHttpJSON(URL);
- if (httpStatus != null && httpStatus.equals("1")) {
- new AlertDialog.Builder(VoteActivity.this).setTitle("success")
- .setMessage("投票成功,非常感谢.").setNeutralButton("返回",
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dlg,
- int sumthin) {
- }
- }).show();
- } else if (httpStatus != null && httpStatus.equals("2")) {
- new AlertDialog.Builder(VoteActivity.this).setTitle("warn")
- .setMessage("投票失败,投票码已经使用.").setNeutralButton("返回",
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dlg,
- int sumthin) {
- }
- }).show();
- } else if (httpStatus != null && httpStatus.equals("0")) {
- new AlertDialog.Builder(VoteActivity.this).setTitle("error")
- .setMessage("投票失败,请联系网管.").setNeutralButton("返回",
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dlg,
- int sumthin) {
- }
- }).show();
- }
- }
- }
- Android Http
- android - http
- android http
- android-http
- Android http
- android http
- android Http
- Android http
- android http
- Android HTTP
- android http
- Android.Http
- Android Http
- android-async-http(http通信)
- [Android]浅析Http框架 - Android-Async-http
- android http post 参数
- android http post请求
- android http上传文件
- Android 引用外部项目库
- 分区引导记录的作用
- ie tester 加载firebug lite
- Linux中的Vi编辑操作
- 全民内核-配置1
- Android http
- OpenWebSpider 爬虫建索引并检索
- IT男改变生活之12306选择上,中,下铺
- iOS常用第三方类库
- open中O_EXCL选项的介绍与分析
- 在C#中实现Socket端口复用
- Ubuntu 12.04安装中文输入法(ibus)
- c++里的system()函数调用系统命令如何获取打印信息
- 硬盘主引导记录,主分区表,分区引导记录,分区链表介绍http://hi.baidu.com/bailyzheng/item/a2cea8f7c0fdf2dc6325d2a8