第八章,POST網絡請求demo(Android)

在 AndroidManifest.xml中添加網絡權限

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/main_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="POST請求" />

    <TextView
        android:id="@+id/main_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
MainActivity.java

package com.example.demo0616;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

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

public class MainActivity extends Activity implements OnClickListener {

	private Button main_button;
	private TextView main_text;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// 初始化控件
		main_button = (Button) this.findViewById(R.id.main_button);
		main_text = (TextView) this.findViewById(R.id.main_text);
		// 爲按鈕設置監聽
		main_button.setOnClickListener(this);
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		// 實例化AsyncTask
		Mytask mt = new Mytask();
		mt.execute();

	}

	// 內部類,繼承AsyncTask
	class Mytask extends AsyncTask<String, String, String> {

		@Override
		protected String doInBackground(String... arg0) {
			// TODO Auto-generated method stub
			HttpClient hc = new DefaultHttpClient();
			HttpPost hp = new HttpPost("http://www.baidu.com");

			// post攜帶的參數
			List<NameValuePair> list = new ArrayList<NameValuePair>();
			list.add(new BasicNameValuePair("uid", "uname"));
			list.add(new BasicNameValuePair("pwd", "111111"));

			try {
				UrlEncodedFormEntity et = new UrlEncodedFormEntity(list,
						HTTP.UTF_8);
				// 把參數放到httppost中
				hp.setEntity(et);
				HttpResponse hr = hc.execute(hp);
				HttpEntity he = hr.getEntity();
				String str = EntityUtils.toString(he, "utf-8");
				publishProgress(str);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return null;
		}

		@Override
		protected void onCancelled(String result) {
			// TODO Auto-generated method stub
			super.onCancelled(result);
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
		}

		@Override
		protected void onProgressUpdate(String... values) {
			// TODO Auto-generated method stub
			super.onProgressUpdate(values);
			// 設置到textview中
			main_text.setText(values[0]);

		}

	}

}
運行截圖:



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章