视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
httppost
2025-10-03 15:09:18 责编:小OO
文档
android平台HttpPost()请求简单实例

Filed Under 学习札记 post by GoER / 2012-2-16 20:50 Thursday 出自网络搜索引擎巨头的Android平台,其对网络的支持自然不用多说,在Android SDK中已经集成了Apache的HttpClient模块。使用HttpClient模块,我们就可以使用HTTP协议进行网络连接了。下面则是使用HttpClient中的HttpPost()方法进行http通信的实例:

view sourceprint?01 /**

02 *description:Android HttpPost()

03 *authour:YanEr·Gates

04 *website:http://www.gogogogo.me

05 */

06 package me.gogogoog;

07

08 import java.io.IOException;

09

10 import java.util.ArrayList;

11 import java.util.List;

12 import org.apache.http.HttpEntity;

13 import org.apache.http.HttpResponse;

14 import org.apache.http.HttpStatus;

15 import org.apache.http.NameValuePair;

16 import org.apache.http.client.ClientProtocolException;

17 import org.apache.http.client.HttpClient;

18 import org.apache.http.client.entity.UrlEncodedFormEntity;

19 import org.apache.http.client.methods.HttpPost;

20 import org.apache.http.impl.client.DefaultHttpClient;

21 import org.apache.http.message.BasicNameValuePair;

22 import org.apache.http.util.EntityUtils;

23 import android.app.Activity;

24 import android.widget.TextView;

25

26 public class ResultActivity extends Activity{

27

28 /** Called when the activity is first created. */

29 @Override

30 public void onCreate(Bundle savedInstanceState){

31 super.onCreate(savedInstanceState);

32 setContentView(R.layout.result);

33

34 TextView resultText = (TextView) this.findViewById(R.id.resultText);

35 String username="username";

36 String password="password";

37 String httpUrl = "http://192.168.1.90:8080/AndroidLogin/loginAction.do?method=login";

38

39 //创建httpRequest对象

40 HttpPost httpRequest = new HttpPost(httpUrl);

41

42 List params = new ArrayList();

43 params.add(new BasicNameValuePair("username

//HttpStatus.SC_OK表示连接成功

60 if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){

61 //取得返回的字符串

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

63 resultText.setText(strResult);

}else{

65 resultText.setText("请求错误!");

66 }

67 }catch (ClientProtocolException e){

68 resultText.setText(e.getMessage().toString());

69 } catch (IOException e){

70 resultText.setText(e.getMessage().toString());

71 }catch (Exception e){

72 resultText.setText(e.getMessage().toString());

73 }

74 }

75 }下载本文

显示全文
专题