在 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]); } } }
运行截图:
时间: 2024-10-30 00:05:25