android http 通信实例(未能正常运行,待后续完善)

主布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="这是一个HTTP通信方式的示例"/>
    <Button android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="通过get方式获取数据"
        android:id="@+id/bt_get"/>
    
    <Button android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="通过post方式获取数据"
        android:id="@+id/bt_post"/>
    
    <TextView android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/tv_content"/>

</LinearLayout>

主活动类HttpMainActivity.java:

package com.example.ch10;

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 com.example.baseexample.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class HttpMainActivity extends Activity {
	private TextView tv_content;
	private Button bt_get,bt_post;
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch10_httpmain);
		
		tv_content = (TextView)findViewById(R.id.tv_content);
		bt_get = (Button)findViewById(R.id.bt_get);
		bt_post = (Button)findViewById(R.id.bt_post);
		
		bt_get.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View v) {
				try{
					final String httpUrl = "http://192.168.1.100:8081";
					HttpGet httpRequest = new HttpGet(httpUrl);
					HttpClient httpClient = new DefaultHttpClient();
					HttpResponse httpResponse = httpClient.execute(httpRequest);
					if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
						String strResult = EntityUtils.toString(httpResponse.getEntity());
						tv_content.setText(strResult);
					}else{
						tv_content.setText("请求错误");
					}
				}catch(Exception e){
					Toast.makeText(HttpMainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
				}
				
			}
			
		});
		
		bt_post.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View v) {
				try{
					final String httpUrl = "http://10.0.2.2:8080/WebDemo/UI/httptest.jsp";
					HttpPost httpRequest = new HttpPost(httpUrl);
					List<NameValuePair> param = new ArrayList<NameValuePair>();
					param.add(new BasicNameValuePair("par", "Post Type:abcdef"));
					HttpEntity entity = new UrlEncodedFormEntity(param,"utf-8");
					httpRequest.setEntity(entity);
					HttpClient httpClient = new DefaultHttpClient();
					HttpResponse httpResponse = httpClient.execute(httpRequest);
					if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
						String strResult = EntityUtils.toString(httpResponse.getEntity());
						tv_content.setText(strResult);
					}else{
						tv_content.setText("请求错误");
					}
				}catch(Exception e){
					Toast.makeText(HttpMainActivity.this, e.getMessage().toString(), Toast.LENGTH_LONG).show();
				}
			}
			
		});
	}
}


添加访问Internet的权限:

<uses-permission android:name="android.permission.INTERNET"/>


转载于:https://my.oschina.net/u/2552902/blog/543850

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值