`
liangoogle
  • 浏览: 273288 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

apache post 连接网络 读取返回数据

阅读更多

 

下面是post的:

public class Test extends Activity implements OnClickListener {

    public Context context;

 

    private TextView textView1;

 

    public static String URL ="http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl";

 

    private DefaultHttpClient httpClient;

 

    private static final int TIMEOUT = 60;

 

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        init();

        try {

 

 

            HttpParams paramsw = createHttpParams();

            httpClient = new DefaultHttpClient(paramsw);

            /* 建立HTTP Post连线 */

            HttpPost post = new HttpPost("http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl");

 

 

//            URI uri = new URI(URL);

//            HttpPost post = new HttpPost();

//            post.setURI(uri);

 

 

            // Post运作传送变数必须用NameValuePair[]阵列储存

            List<NameValuePair> params = new ArrayList<NameValuePair>();

            params.add(new BasicNameValuePair("name", "this is post"));

//            params.add(new BasicNameValuePair("version", getVersionCode(context) + ""));

//            params.add(new BasicNameValuePair("os", "android"));

//            params.add(new BasicNameValuePair("flag", "10"));

//            params.add(new BasicNameValuePair("modle", getModelNumber()));

//            params.add(new BasicNameValuePair("channel", "samsung"));

 

 

 

            try {

 

                // 发出HTTP request,把字符集设置在request里面

                post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

                 //相当于打开链接,给出回应

                HttpResponse httpResponse = httpClient.execute(post);

 

                int httpCode = httpResponse.getStatusLine().getStatusCode();

 

 

                //HTTP_OK=200,服务器作出反映,找到了服务器,并且回应了。

                if (httpCode == HttpURLConnection.HTTP_OK) {

 

//                  //取出回应字串

//                    String strResult=EntityUtils.toString(httpResponse.getEntity());

//                    textView1.setText(strResult);

 

                   //这里可以做很多事 

                    Header[] headers=httpResponse.getAllHeaders();

                    HttpEntity entity=httpResponse.getEntity();

                    Header header=httpResponse.getFirstHeader("content-type");

 

                    //下面是从流中把字符串读出来了

                    InputStream inputStream=entity.getContent();

                    InputStreamReader inputStreamReader=new InputStreamReader(inputStream);

                    BufferedReader reader = new BufferedReader(inputStreamReader);//读字符串用的。

                    String inputLine = null;

                    String result = "";

                    // 使用循环来读取获得的数据,把数据都村到result中了

                    while (((inputLine = reader.readLine()) != null)) {

                        // 我们在每一行后面加上一个"\n"来换行

                        result += inputLine + "\n";

                    }

                    reader.close();//关闭输入流

 

 

 

 

                }else {

                    textView1.setText("Error Response"+httpResponse.getStatusLine().toString());

                }

 

 

 

 

            } catch (UnsupportedEncodingException e) {

                textView1.setText("网络连接错误UnsupportedEncodingException");

            } catch (ClientProtocolException e) {

                textView1.setText("网络连接错误ClientProtocolException");

            } catch (IOException e) {

                textView1.setText("网络连接错误IOException");

            }

        } finally {

 

            if (httpClient != null) {

                httpClient.getConnectionManager().shutdown();//最后关掉链接。

                httpClient = null;

            }

        }

    }

    /**

     * 手机型号

     * 

     * @return

     */

    private String getModelNumber() {

        String modelNo = "Unknown";

        try {

            modelNo = Build.MODEL;

        } catch (Exception e) {

            // ignored

        }

        return modelNo;

    }

 

    /**

     * 获得版本CODE

     * 

     * @param context

     * @return

     */

    public static int getVersionCode(Context context) {

        PackageManager pm = context.getPackageManager();

        try {

            PackageInfo info = pm.getPackageInfo(context.getPackageName(), 0);

            if (info != null) {

                return info.versionCode;

            }

        } catch (NameNotFoundException e) {

            e.printStackTrace();

        }

 

        return 1;

    }

 

    /**

     * Create the default HTTP protocol parameters.

     */

    public static final HttpParams createHttpParams() {

        final HttpParams params = new BasicHttpParams();

        // Turn off stale checking. Our connections break all the time anyway,

        // and it's not worth it to pay the penalty of checking every time.

        HttpConnectionParams.setStaleCheckingEnabled(params, false);

        //设置链接超时,socket超时,以及socket大小。

        HttpConnectionParams.setConnectionTimeout(params, TIMEOUT * 1000);

        HttpConnectionParams.setSoTimeout(params, TIMEOUT * 1000);

        HttpConnectionParams.setSocketBufferSize(params, 8192 * 5);

        return params;

    }

    private void init() {

        // TODO Auto-generated method stub

        textView1 = (TextView) findViewById(R.id.txt);

        Button button1 = (Button) findViewById(R.id.button1);

        Button button2 = (Button) findViewById(R.id.button2);

        Button button3 = (Button) findViewById(R.id.button3);

        button1.setOnClickListener(this);

        button2.setOnClickListener(this);

        button3.setOnClickListener(this);

    }


分享到:
评论

相关推荐

    android 常用工具类

    JSONUtils工具类,可用于方便的向Json中读取和写入相关类型数据,如: String getString(JSONObject jsonObject, String key, String defaultValue) 得到string类型value String getString(String jsonData, ...

    druid-prometheus-exporter:用于收集 Apache Druid 指标并将其导出到 Prometheus 的服务

    此服务仅公开两个端点: /collect /metrics收集端点此端点是一个POST ,您可以在其中指示 Druid 将指标发送到。 端点将解析每个feed并更新相应的Prometheus指标。指标端点prometheus 客户端公开此端点上的所有指标...

    httpClient

    // 读取为 InputStream,在网页内容数据量大时候推荐使用 // InputStream response = getMethod.getResponseBodyAsStream(); } catch (HttpException e) { // 发生致命的异常,可能是协议不对或者返回的内容有...

    达内客户端+聊天室源码

    // 读取返回数据信息 String str = EntityUtils.toString(response.getEntity()); System.out.println(str); if(str.indexOf("true")!=-1){ return true; }else{ return false; } }...

    HttpClient以及获取页面内容应用

    13. Response的输入流可以有效的从socket服务器直接读取相应内容。 14. 在http1.0和http1.1中利用KeepAlive保持持久连接。 15. 直接获取服务器发送的response code和 headers。 16. 设置连接超时的能力。 17. 实验性...

    PHP网络编程技术与实践 源码

    1.2 Linux下Apache的安装配置 1.2.1 Apache的安装工作 1.2.2 Apache的配置工作 1.3 PHP的安装配置 1.3.1 Windows系统中PHP的安装 1.3.2 Linux系统中PHP的安装 1.4 MySQL数据库的安装配置 1.4.1 在Windows下安装MySQL...

    用HttpClient来模拟浏览器GET POST

    例如说我们想知道某个手机号码的归属地而我们自己又没有这样的数据,因此只好借助其他公司已有的网站来完成这个功能,这个时候我们需要向网页提交手机号码并从返回的页面中解析出我们想要的数据来。如果对方仅仅是一...

    Android网络请求之OkHttp

    OkHttp是一款优秀的HTTP框架,它支持get请求和post请求,支持基于Http的文件上传和下载,支持加载图片,支持下载文件透明的GZIP压缩,支持响应缓存避免重复的网络请求,支持使用连接池来降低响应延迟问题 ...

    php网络开发完全手册

    1.5 几种综合网络服务器系统的安装 14 1.5.1 XAMPP 14 1.5.2 WAMP 16 1.5.3 Appserv 17 1.5.4 EasyPHP 18 1.5.5 VertrigoServ 19 1.6 几种开发工具的介绍 20 1.6.1 Vi及Vim 20 1.6.2 Eclipse+PHPEclipse插件 21 ...

    PHP程序开发范例宝典III

    实例121 通过POST方法提交 182 实例122 通过GET方法提交 183 实例123 通过SESSION实现页面间的数据传递 184 5.2 页面跳转 185 实例124 使用header()函数实现页面跳转 185 实例125 应用HTML标记进行跳转 ...

    httpclient4.5发送https请求(验证证书)

    不会的可以在评论区留言哈,这是我自己做项目用到的。所以绝对可用!同时共享出来给到大家

    PHP开发实战1200例(第1卷).(清华出版.潘凯华.刘中华).part1

    实例068 SESSION购物车中数据的读取 100 实例069 员工信息的管理 102 实例070 网页版九九乘法表 104 实例071 读取数组购物车中的数据 105 实例072 图像验证码的生成 106 2.6 跳转语句 107 实例073 控制页面中表情图...

    PHP开发实战1200例(第1卷).(清华出版.潘凯华.刘中华).part2

    实例068 SESSION购物车中数据的读取 100 实例069 员工信息的管理 102 实例070 网页版九九乘法表 104 实例071 读取数组购物车中的数据 105 实例072 图像验证码的生成 106 2.6 跳转语句 107 实例073 控制页面中表情图...

    Android静默安装常用工具类

    Http网络工具类,主要包括httpGet、httpPost以及http参数相关方法,以httpGet为例: static HttpResponse httpGet(HttpRequest request) static HttpResponse httpGet(java.lang.String httpUrl) static String ...

    Java实现批量下载并压缩文件.pptx.pptx

    Java中可以使用HttpURLConnection或者Apache HttpClient等工具类进行文件下载,通过设置请求头和请求参数,发送get或post请求获取文件。 使用Java压缩多个文件为zip包 Java中可以使用ZipOutputStream类将多个文件...

    谈谈Android的三种网络通信方式

    Android平台有三种网络接口可以使用,他们分别是:java.net.*(标准Java接口)、Org.apache接口和Android.net.*(Android网络接口)。下面分别介绍这些接口的功能和作用。 1.标准Java接口 java.net.*提供与联网有关的...

    ImageMagick-7.1.0-14.tar.gz

    它可以读取和写入各种格式(超过200种)的图像,包括PNG,JPEG,GIF,HEIC,TIFF,DPX,EXR,WebP,Postscript,PDF和SVG。使用ImageMagick可以调整图像大小,翻转,镜像,旋转,变形,剪切和变换图像,调整图像颜色...

    nyla-spring-postit:PostIt是一个基于Spring Boot Web的邮件客户端

    PostIt是一个基于Spring Boot Web的邮件客户端。 它使用Apache Geode / GemFire中加载的用户数据库。启动启动Geode“ gfsh”工具gfsh首先使用Gfsh的Geode定位器和缓存服务器gfsh&gt;start locator --name=local --...

    ImageMagick7.0.8 win64 16安装包下载。使用moviepy抛ImageMagick异常问题解决

    它可以读取和写入各种格式(超过200种)的图像,包括PNG,JPEG,GIF,HEIC,TIFF,DPX,EXR,WebP,Postscript,PDF和SVG。使用ImageMagick可以调整图像大小,翻转,镜像,旋转,变形,剪切和变换图像,调整图像颜色...

    Ajax+PHP简单数据交互

    PHP是一种创建动态交互性站点的服务器端脚本语言,优势:PHP脚本语言应用广泛,开源免费,最重要的是入门简单,容易掌握。...所以需要下载个XAMPP,我们在百度搜索apache friends,直接打开第一个链接,然后毫不

Global site tag (gtag.js) - Google Analytics