/HttpGet 方式

 import java.io.IOException; importimport java.io.IOException;
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 org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;public class Activity02 extends Activity
{/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.http);TextView mTextView = (TextView) this.findViewById(R.id.TextView_HTTP);// http地址String httpUrl = "http://192.168.1.110:8080/httpget.jsp?par=HttpClient_android_Get";//HttpGet连接对象HttpGet httpRequest = new HttpGet(httpUrl);try{//取得HttpClient对象HttpClient httpclient = new DefaultHttpClient();//请求HttpClient,取得HttpResponseHttpResponse httpResponse = httpclient.execute(httpRequest);//请求成功if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){//取得返回的字符串String strResult = EntityUtils.toString(httpResponse.getEntity());mTextView.setText(strResult);}else{mTextView.setText("请求错误!");}}catch (ClientProtocolException e){mTextView.setText(e.getMessage().toString());}catch (IOException e){mTextView.setText(e.getMessage().toString());}catch (Exception e){mTextView.setText(e.getMessage().toString());}  //设置按键事件监听Button button_Back = (Button) findViewById(R.id.Button_Back);/* 监听button的事件信息 */button_Back.setOnClickListener(new Button.OnClickListener() {public void onClick(View v){/* 新建一个Intent对象 */Intent intent = new Intent();/* 指定intent要启动的类 */intent.setClass(Activity02.this, Activity01.class);/* 启动一个新的Activity */startActivity(intent);/* 关闭当前的Activity */Activity02.this.finish();}});}
}

/HttpPost 方式//

package com.yarin.android.Examples_08_02;import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;public class Activity03 extends Activity
{/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.http);TextView mTextView = (TextView) this.findViewById(R.id.TextView_HTTP);// http地址String httpUrl = "http://192.168.1.110:8080/httpget.jsp";//HttpPost连接对象HttpPost httpRequest = new HttpPost(httpUrl);//使用NameValuePair来保存要传递的Post参数List<NameValuePair> params = new ArrayList<NameValuePair>();//添加要传递的参数params.add(new BasicNameValuePair("par", "HttpClient_android_Post"));try{//设置字符集HttpEntity httpentity = new UrlEncodedFormEntity(params, "gb2312");//请求httpRequesthttpRequest.setEntity(httpentity);//取得默认的HttpClientHttpClient httpclient = new DefaultHttpClient();//取得HttpResponseHttpResponse httpResponse = httpclient.execute(httpRequest);//HttpStatus.SC_OK表示连接成功if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){//取得返回的字符串String strResult = EntityUtils.toString(httpResponse.getEntity());mTextView.setText(strResult);}else{mTextView.setText("请求错误!");}}catch (ClientProtocolException e){mTextView.setText(e.getMessage().toString());}catch (IOException e){mTextView.setText(e.getMessage().toString());}catch (Exception e){mTextView.setText(e.getMessage().toString());}  //设置按键事件监听Button button_Back = (Button) findViewById(R.id.Button_Back);/* 监听button的事件信息 */button_Back.setOnClickListener(new Button.OnClickListener() {public void onClick(View v){/* 新建一个Intent对象 */Intent intent = new Intent();/* 指定intent要启动的类 */intent.setClass(Activity03.this, Activity01.class);/* 启动一个新的Activity */startActivity(intent);/* 关闭当前的Activity */Activity03.this.finish();}});}
}

转载于:https://www.cnblogs.com/lm3515/archive/2011/03/30/2000340.html

HttpClient 联网接口相关推荐

  1. httpclient 调取接口_使用HttpClient调用接口的实例讲解

    一,编写返回对象 public class HttpResult { // 响应的状态码 private int code; // 响应的响应体 private String body; get/se ...

  2. 精短高效的XML解析器,纯C单一程序,应用于银行的国税库行横向联网接口系统中,稳定可靠,运行速度飞快

    来源于:http://www.ej38.com/showinfo/c-xml-169407.html 供各位参考 精短高效的XML解析器,纯C单一程序,应用于银行的国税库行横向联网接口系统中,稳定可靠 ...

  3. HttpClient远程接口调用-实名认证

    1.HttpClient远程接口调用 1)用户注册 注册按钮button提交表单时,要return false form表单 <!-- action="http://localhost ...

  4. httpclient+testng接口自动化框架二次封装Java

    倒叙看,最新的在上面.... 框架设计见我的另一个博客https://blog.csdn.net/weixin_42498050/article/details/115671411 开发不规范的地方: ...

  5. httpClient请求接口,用httpEntity接受结果

    标题 包含get和post的有参和无参的请求 post有参请求用httpEntity接受,处理成JsonObject类型 方法 package com.shzu.xyf.finding_sql_ins ...

  6. httpclient 调取接口_HttpClient调用接口发送文件

    客户端发送文件: /** * 文件上传 * @param remote_url 请求地址 * @param file 请求文件 * @return 响应结果 */ public String http ...

  7. 路由器有以下几种联网接口

    2019年5月3日 星期五 补 最后一次,不要再补了! 1.RJ-54端口:在这种端口上通过双绞线连接以太网. 10Base-T的RJ-45端口标志为"ETH",而100Base- ...

  8. 基于HttpClient接口开发实例(一)

    前言 本系列将是本人的一些开发经验总结.由于身处金融IT因而因业务需求需要和银行接口打交道,恰巧最近公司有个项目和这有很大的关系,下面我们将着重从基于HttpClient接口开发的技术点+基本业务点说 ...

  9. HttpClient 4.3教程(转载) 转自:http://www.yeetrack.com/

    HttpClient 4.3教程(转载) 转自:http://www.yeetrack.com/?p=779 前言 Http协议应该是互联网中最重要的协议.持续增长的web服务.可联网的家用电器等都在 ...

最新文章

  1. 已经使用过的rails插件
  2. busybox在android开发中的应用
  3. 一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。...
  4. TCP/IP协议簇简介
  5. 第一阶段unity基础
  6. delphi XE 學習筆記二:TThread.CreateAnonymousThread
  7. 如何用JavaScript搭建神经网络?90分钟免费课程,上手可操作丨教程
  8. 将摄像头拍摄图像或者本地图片设置为头像的方法
  9. Swift 4 和 Objective-C 在同一个工程里的混搭编程的方法
  10. css3弹性伸缩和使用
  11. Nhibernate(1)续
  12. hdu1023-----卡特兰数
  13. 基于proteus的CPU控制器设计(微程序版)
  14. 使用Python开发游戏运行脚本(三)图片查找
  15. delphi mysql 加密_Delphi纯代码连SQLite数据库,同时支持数据库的加密解密
  16. 柜台收取西联汇款经验(网友分享)
  17. 软件综合实践专题作业--墨刀的使用
  18. 《人月神话》-人月神话
  19. Solidworks中螺纹怎么收尾?
  20. 写给大家看的机器学习书【Part3】直观易懂的感知机学习算法PLA

热门文章

  1. c语言赋值小数,c语言中将一个浮点型赋值给整型时,会不会四舍五入?
  2. 服务器版的mysql怎么装_WIN7服务器配置之MySQL数据库安装图解(适用于5.1,5.5的版本)...
  3. java创建对象new后面为啥可以传入参数_来复习一下Java的对象知识
  4. c++ substr截取中间部分字符串_杭电OJ 字符串处理类部分题解
  5. 启动非java虚拟机方式下运行matlab
  6. 数据集-用于数据挖掘、信息检索、知识发现等
  7. 腾讯2019暑期实习生提前批CV岗笔试题
  8. 路径包含空格_5分钟学会:矢量工具与路径-编辑路径
  9. 电脑故障检测软件_有什么软件可以检测出电脑配置是否达到游戏配置。
  10. T-SQL Recipes之Customized Database Objects