由于网络访问需要时间,故放入子线程,并通过handler在UI线程(主线程)中进行显示的更新:

MainActivity.java:

package com.example.isns;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.text.Html;import android.text.method.ScrollingMovementMethod;import android.widget.TextView;import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.ClientProtocolException;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;public class MainActivity extends Activity {private static final int MSG_SUCCESS = 0;private static final int MSG_FAILURE = 1;private TextView t;private String result;private Handler handler = new Handler(){public void handleMessage(Message msg){switch(msg.what){case MSG_SUCCESS:t.setMovementMethod(ScrollingMovementMethod.getInstance());t.setText(Html.fromHtml(result));break;case MSG_FAILURE:t.setText("fail");break;}}};    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        t = (TextView)findViewById(R.id.tv);    Post();    }    private void Post(){    new Thread(){    public void run(){    String url = "http://www.w3school.com.cn/example/php/demo_php_global_post.php";        HttpPost httpPost = new HttpPost(url);        List<NameValuePair> params = new ArrayList<NameValuePair>();        params.add(new BasicNameValuePair("fname","ISNS"));        HttpResponse httpResponse = null;        try{        httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));        httpResponse = new DefaultHttpClient().execute(httpPost);        if(httpResponse.getStatusLine().getStatusCode() == 200){        result = EntityUtils.toString(httpResponse.getEntity());        handler.obtainMessage(MSG_SUCCESS).sendToTarget();        }        }catch(ClientProtocolException e){        e.printStackTrace();        }catch(IOException e){        e.printStackTrace();        }    }    }.start();    }}

activity_main.xml:

<RelativeLayout 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"    tools:context="${relativePackage}.${activityClass}" >    <TextView        android:id="@+id/tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" /></RelativeLayout>

要记得在manifest中加入网络访问权限:

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


更多相关文章

  1. Android中的HandlerThread和IntentService
  2. AndroidO Camera 分析(一): (API 2)openCamera 流程
  3. Android(安卓)从缓存中读取图片并异步加载的类[转]
  4. Android:网络框架android-async-http 1.4.6-1.4.9更新概述
  5. Android(安卓)网络通信框架Volley简介
  6. Android延时执行方法
  7. 【自用】Android中一个简单的计时器,用Handler加Runnable线程实现
  8. android中异步加载网络图片
  9. Android(安卓)OKHTTP 网络请求出错重连---http拦

随机推荐

  1. android sd卡数据库读取
  2. Android 开发之官方文档ActionBar学习记
  3. 谷歌发布基于机器学习的Android(安卓)APP
  4. [Android]RIL的启动流程
  5. 64位 Ubuntu android studio 编译android
  6. Android中Linux睡眠唤醒流程
  7. Android官方命令深入分析之bmgr
  8. Android scrollview监听滑动状态
  9. 部分可点击的TextVie
  10. Android架构师:Android零基础进阶到高级