关于动态更新 安卓 TExtview的 代码

package com.example.newtest;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

public class addressActivity extends Activity{
 
 private TextView address_location;

 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.address_activity);
  address_location = (TextView)findViewById(R.id.address_now);
  
  address_location.setText("定位中...");
   new Thread(){
          public void run(){
            HttpClientUtil httpClient = new HttpClientUtil();
           
      
      String getjsonstring = httpClient.getJsonContent("http://ip.taobao.com/service/getIpInfo2.php?ip=myip").toString();
      
      try {
       final JSONObject obj = new JSONObject(getjsonstring);
           final String token = obj.getString("data");
       
       try {
         final JSONObject obj2 = new JSONObject(token);
         String token1 = obj2.getString("region");
         String token2 = obj2.getString("city");
         
            SharedPreferences settings = getSharedPreferences("persondata",MODE_PRIVATE);
       SharedPreferences.Editor editor = settings.edit();
       Message message = new Message();
       message.what = 1;
       Bundle bundle = new Bundle();  
       bundle.putString("region",token1);  //往Bundle中存放数据 
       bundle.putString("city",token2);  //往Bundle中存放数据 
       message.setData(bundle);//mes利用Bundle传递数据  
       handler.sendMessage(message);
       editor.putString("region",token1);
       editor.putString("city",token2);
       editor.commit();
        } catch (JSONException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        }
      } catch (JSONException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }  
      
          }
         }.start();        
      
       
      
      
   
 }
 private Handler handler = new Handler(){
  public void handleMessage(Message msg){
   String str1 = msg.getData().getString("region");//接受msg传递过来的参数 
   String str2 = msg.getData().getString("city");//接受msg传递过来的参数
   address_location = (TextView)findViewById(R.id.address_now);
   
   address_location.setText(str1+" "+ str2);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值