`

android网络编程(五)HttpClient POST

阅读更多
运行效果图:

[img]

[/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>
  • 大小: 82.8 KB
分享到:
评论

相关推荐

    android网络编程基础

    在Android平台上进行网络编程是开发应用不可或缺的一部分,它允许应用程序与远程服务器进行数据交互,实现如下载、上传、同步等功能。本节将深入探讨Android网络编程的基础知识,主要围绕Java网络编程这一核心概念...

    Http(get)请求数据Android Studio使用HttpClient

    总之,了解如何使用`HttpClient`进行GET请求是Android开发的基础技能,但随着技术的发展,建议学习和使用更现代的网络库,以提升应用的效率和维护性。在实际项目中,务必考虑兼容性和最佳实践,以确保应用程序的稳定...

    android网络请求框架 HttpClient与Volley的性能对比

    在Android开发中,进行网络请求是应用程序不可或缺的一部分。在过去的版本中,HttpClient是官方推荐的网络请求库,但随着技术的发展,Volley逐渐崭露头角,成为许多开发者的新选择。本文将对HttpClient和Volley这两...

    android网络编程.pdf

    ### Android网络编程基础知识点 #### 一、网络状态检测 在Android开发中,为了确保应用程序能够根据当前网络环境做出响应,通常需要先检测设备的网络连接状态。这可以通过`ConnectivityManager`类来实现。 1. **...

    Android网络编程上传下载postGet请求

    在Android开发中,网络编程是不可或缺的一部分,尤其是涉及到与服务器交互时,如上传和下载数据。本篇文章将详细探讨Android中的HTTP请求,包括GET、POST方法的使用,以及上传和下载文件的实现策略。 首先,GET和...

    android httpClient

    尽管如此,对于理解Android网络编程的基础和历史,HttpClient仍然是一个重要的知识点。 标题“android httpClient”指出我们要讨论的是Android环境下使用HttpClient进行网络通信的相关技术。HttpClient允许开发者...

    android中Http和HttpClient的Get和Post方法的使用

    加载图片是Android网络编程中的常见任务,通常涉及异步处理以避免阻塞UI线程。有多种策略可以实现: 1. 直接在UI线程之外加载:使用`AsyncTask`或者自定义线程池来加载图片,然后更新UI。 2. 使用库进行优化:如` ...

    Android网络编程之Http通信

    Android网络编程是移动应用开发中的一个重要组成部分,而Http通信则是网络编程中最常用的协议之一。在Android平台上,我们通常会使用HttpURLConnection或者第三方库如Apache HttpClient和OkHttp等进行Http通信。 1....

    Android HttpClient源码

    总的来说,理解Android HttpClient的源码能够帮助我们更好地掌握网络请求的处理,提升应用程序的性能和可靠性。尽管现在推荐使用其他替代方案,但HttpClient仍然是一个学习HTTP通信和网络编程的宝贵资源。通过分析和...

    android httpclient demo

    在Android开发中,HTTP通信是应用与服务器交互的重要方式之一,`Android HttpClient`库就是用于实现这...虽然HttpClient已逐渐被其他更现代的库取代,但它仍然是理解HTTP请求原理和实践Android网络编程的一个良好起点。

    android4.0网络编程详解——源码

    在Android 4.0(Ice Cream Sandwich)系统中,网络编程是实现应用程序与远程服务器交互的重要部分。本章将深入探讨如何在Android平台上进行网络操作,包括基础的HTTP请求、数据传输、网络状态监听以及更高级的Socket...

    Android4.0网络编程详解.pdf

    但是,基于标题“Android4.0网络编程详解.pdf”,我们可以详细探讨Android 4.0版本中的网络编程相关知识点。 Android 4.0,代号为“冰激凌三明治”,是Google在2011年发布的一个重要的Android操作系统版本,它整合...

    Android网络编程 王家林

    《Android.4.0网络编程详解》是王家林撰写的一本专著,主要针对Android平台上的网络编程技术进行了深入的探讨。这本书对于开发者来说,是掌握Android应用中网络功能实现的重要参考资料。以下是对该书内容的详细解读...

    Android HttpClient Network Lib

    在Android开发中,网络通信是应用功能不可或缺的一部分。HttpClient库为开发者提供了一种方便、...虽然HttpClient已不再是最推荐的网络库,但它仍然是Android网络编程历史上的一个重要里程碑,值得我们去了解和学习。

    android 网络编程详解代码第7章(android的Socket编程)

    对于更高级的网络编程,如HTTP、HTTPS协议,Android提供了HttpURLConnection、HttpClient(已被弃用)、OkHttp等库。这些库可以帮助我们更容易地处理HTTP请求和响应,例如GET、POST、PUT、DELETE等操作。但是,如果...

    android网络编程之通讯源代码

    在Android平台上进行网络编程是开发移动应用不可或缺的一部分。"Android网络编程之通讯源代码"着重于展示如何在Android应用中实现实时的数据交换,这对于构建功能丰富的应用如社交媒体、在线购物、实时聊天等至关...

    httppost和httpclient一组开发包

    在Android开发中,HTTP通信是应用与服务器交互的基础,`HttpClient`和`HttpPost`就是其中常用的两个类,它们属于Apache的HTTP组件,虽然在Android API level 23之后被标记为过时,但由于其功能强大和灵活性高,仍被...

Global site tag (gtag.js) - Google Analytics