android的http请求

1.xml界面代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <TextView
        android:id="@+id/myWebTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

    <Button
        android:id="@+id/requestBtn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Send Request Get"  />

    <Button
        android:id="@+id/requestBtnPost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Send Request POST"/>

    <WebView
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/requestBtn"
        android:layout_below="@id/myWebTitle" />

</LinearLayout>
布局代码

2.java代码

package com.example.wang.myhttp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Wang on 2015/8/22.
 */
public class HttpActivity extends Activity {
    private Button mSendReqBtn = null;// 发送请求的按钮
    private WebView mWebView = null;// 用于显示结果,用载入html字符串的方式显示响应结果,而不是使用WebView自己的方式加载URL

    // 响应
    private HttpResponse mHttpResponse = null;
    // 实体
    private HttpEntity mHttpEntity = null;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_http_demo1);

        mSendReqBtn = (Button) findViewById(R.id.requestBtn);
        mSendReqBtn.setOnClickListener(mSendClickListener);
        Button PostBtn = (Button)findViewById(R.id.requestBtnPost);
        PostBtn.setOnClickListener(PostListener);

        mWebView = (WebView) findViewById(R.id.webview);
    }


    private View.OnClickListener PostListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                HttpPost httpPost = new HttpPost("http://192.168.0.180:1919/default.aspx");
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("Name", "15632"));
                httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));//添加参数
                HttpClient httpClient = new DefaultHttpClient();
                InputStream inputStream = null;
                mHttpResponse = httpClient.execute(httpPost);
                mHttpEntity = mHttpResponse.getEntity();

                // 获取一个输入流
                inputStream = mHttpEntity.getContent();

                BufferedReader bufferedReader = new BufferedReader(
                        new InputStreamReader(inputStream));

                String result = "";
                String line = "";

                while (null != (line = bufferedReader.readLine()))
                {
                    result += line;
                }

                // 将结果打印出来,可以在LogCat查看
                System.out.println(result);

                // 将内容载入WebView显示
                mWebView.getSettings().setDefaultTextEncodingName("UTF-8");
                // 直接使用mWebView.loadData(result, "text/html", "utf-8");会显示找不到网页

                // 换成下面的方式可以正常显示(但是比较宽,拖动可见百度logo)
                mWebView.loadDataWithBaseURL(null, result, "text/html",
                        "utf-8", null);

                // 直接载入URL也可以显示页面(但是此例子主要是为了验证响应返回的字符串是否正确,所以不用下面这行代码)
                // mWebView.loadUrl("http://www.baidu.com/");


            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    };


    private View.OnClickListener mSendClickListener = new View.OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            // 生成一个请求对象
            HttpGet httpGet = new HttpGet("http://192.168.0.180:1919/default.aspx?Name=156");
            // 生成一个Http客户端对象
            HttpClient httpClient = new DefaultHttpClient();

            // 下面使用Http客户端发送请求,并获取响应内容

            InputStream inputStream = null;
            try
            {
                // 发送请求并获得响应对象
                mHttpResponse = httpClient.execute(httpGet);
                // 获得响应的消息实体
                mHttpEntity = mHttpResponse.getEntity();

                // 获取一个输入流
                inputStream = mHttpEntity.getContent();

                BufferedReader bufferedReader = new BufferedReader(
                        new InputStreamReader(inputStream));

                String result = "";
                String line = "";

                while (null != (line = bufferedReader.readLine()))
                {
                    result += line;
                }

                // 将结果打印出来,可以在LogCat查看
                System.out.println(result);

                // 将内容载入WebView显示
                mWebView.getSettings().setDefaultTextEncodingName("UTF-8");
                // 直接使用mWebView.loadData(result, "text/html", "utf-8");会显示找不到网页

                // 换成下面的方式可以正常显示(但是比较宽,拖动可见百度logo)
                mWebView.loadDataWithBaseURL(null, result, "text/html",
                        "utf-8", null);

                // 直接载入URL也可以显示页面(但是此例子主要是为了验证响应返回的字符串是否正确,所以不用下面这行代码)
                // mWebView.loadUrl("http://www.baidu.com/");
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            finally
            {
//                try
//                {
//                  //  inputStream.close();
//                }
//                catch (IOException e)
//                {
//                    e.printStackTrace();
//                }
            }

        }
    };
}
android类代码

3.建立IIS的WEB服务

 

posted @ 2015-08-22 17:42  聆听的风声  阅读(156)  评论(0)    收藏  举报