android网络编程(五)HttpClient POST

运行效果图:

[img]
android网络编程(五)HttpClient POST
[/img]

package com.amaker.http4;

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.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.util.EntityUtils;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
/**
 * HttpClient
 * POST
 * @author zzl
 *
 */
public class MainActivity extends Activity {
	private TextView tv;
	private Button btn;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		tv = (TextView) findViewById(R.id.tv);
		btn = (Button) findViewById(R.id.button1);

		btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					requestByPost();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public String requestByPost() throws Exception {
		String strResult = "";
		// http地址
		String httpUrl = "http://open.ifenghui.cn/mobilecms/interface/mobile.action?";
		// HttpPost连接对象
		HttpPost httpRequest = new HttpPost(httpUrl);
		// 使用NameValuePair来保存要传递的Post参数
		// 要上传的参数:code=888032100100&m=get_coolimages&p=1&filter=
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		// 添加要传递的参数
		params.add(new BasicNameValuePair("code", "888032100100"));
		params.add(new BasicNameValuePair("m", "get_coolimages"));
		params.add(new BasicNameValuePair("p", "1"));
		params.add(new BasicNameValuePair("filter", "all"));
		// 设置字符集
		HttpEntity httpentity = new UrlEncodedFormEntity(params, "gb2312");
		// 请求httpRequest
		httpRequest.setEntity(httpentity);
		// 取得默认的HttpClient
		HttpClient httpclient = new DefaultHttpClient();
		// 取得HttpResponse
		HttpResponse httpResponse = httpclient.execute(httpRequest);
		// HttpStatus.SC_OK表示连接成功
		if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			// 取得返回的字符串
			strResult = EntityUtils.toString(httpResponse.getEntity());
			tv.setText(strResult);
		} else {
			tv.setText("请求错误!");
		}
		return strResult;
	}

}



main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<ScrollView
		android:id="@+id/sv"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		xmlns:android="http://schemas.android.com/apk/res/android"
		android:layout_x="0px"
		android:layout_y="20px">
		<TextView
			android:layout_width="fill_parent"
			android:layout_height="wrap_content"
			android:id="@+id/tv"
			android:text="@string/hello" />
	</ScrollView>
	<Button
		android:text="HttpClient-POST"
		android:id="@+id/button1"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content" />
</LinearLayout>



AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.amaker.http4"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name="com.amaker.http4.MainActivity"
                  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-permission android:name="android.permission.INTERNET"></uses-permission> 
</manifest>

你可能感兴趣的:(httpclient,http)