android http 本地 web服务(tomcat)

本文详细介绍了Android应用中如何进行网络请求及配置网络权限,包括使用DefaultHttpClient进行POST请求,封装数据,并处理响应结果。

1. Android代码

public class JobActivity extends Activity {
	private TextView show;
	private EditText txt;
	private Button btn;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        ConnectivityManager con=(ConnectivityManager)getSystemService(Activity.CONNECTIVITY_SERVICE);
        boolean wifi=con.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
        boolean internet=con.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
        if(wifi ){
        	show = (TextView)findViewById(R.id.show);
            txt = (EditText)findViewById(R.id.txt);
            btn = (Button)findViewById(R.id.btn);
            btn.setOnClickListener(new OnClickListener() {
        	@Override
        	   public void onClick(View v) {
        		show.setText("dianjile");
        	    dopost(txt.getText().toString());
        	   }
        	  });
        }
    }
    
    private void dopost(String val){
        //封装数据
        Map<String, String> parmas = new HashMap<String, String>();
        parmas.put("name", val);
       

DefaultHttpClient client = new DefaultHttpClient();//http客户端

//这个地方需要注意:如果是调试器 10.0.2.2 本机地址;真机上:则是路由器中电脑地址

HttpPost httpPost = new HttpPost("http://192.168.2.100:80/loowj/android$Android$go"); ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>(); if(parmas != null){ Set<String> keys = parmas.keySet(); for(Iterator<String> i = keys.iterator(); i.hasNext();) { String key = (String)i.next(); pairs.add(new BasicNameValuePair(key, parmas.get(key))); } } try { UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(pairs, "utf-8"); /* * 将POST数据放入HTTP请求 */ httpPost.setEntity(p_entity); /* * 发出实际的HTTP POST请求 */ TextView tv = (TextView)findViewById(R.id.state); tv.setText("start post:"); HttpResponse response = client.execute(httpPost); tv.setText((response.getStatusLine().getStatusCode() +"")); HttpEntity entity = response.getEntity(); InputStream content = entity.getContent(); String returnConnection = convertStreamToString(content); show.setText(returnConnection); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private String convertStreamToString(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line); } } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); } }

2.android 配置文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.loowj.core"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />
     //注意加上网络权限     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
	<uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name="loowj.JobActivity"
            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>

</manifest>


转载于:https://my.oschina.net/wjgood/blog/42487

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值