13 Android AsyncHttpClient 使用

package com.sd.ert;

import java.io.IOException;

import org.apache.http.Header;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import android.app.Activity;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class TBtnActivity extends Activity {
    /** Called when the activity is first created. */
	private TextView textview;
	private ProgressBar progressBar;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textview=(TextView)findViewById(R.id.textview1);
        progressBar=(ProgressBar)findViewById(R.id.progressBar1);
        
       
        progressBar.setVisibility(View.INVISIBLE);
        
        
       
    }
    
    public void myClick(View view)
    {
    	 initView("http://m.weather.com.cn/data/101010100.html");
    	Toast.makeText(TBtnActivity.this, "123abc", Toast.LENGTH_LONG).show();
        Log.d("ERROcuowu","______________cas");
    }
    
    public void initView(String url)
    {
    	TextView textView=(TextView)findViewById(R.id.textview1);
    	
    	RequestParams params=new RequestParams();
    	params.put("id", 1+"");
    	AsyncHttpClient ahc=new AsyncHttpClient();
    	ahc.get(url, new AsyncHttpResponseHandler()
    	{
    		
    		@Override
			public void onFailure(Throwable error, String content) {
				// TODO Auto-generated method stub
				super.onFailure(error, content);
				
				Toast.makeText(TBtnActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
			}

			public void onSuccess(String reult)
    		{

    			textview.setText(reult);
    			Toast.makeText(TBtnActivity.this, "请求成功", Toast.LENGTH_SHORT).show();
    			
    		}

			@Override
			public void onFinish() {
				// TODO Auto-generated method stub
				super.onFinish();
				   /* 调用setIndeterminate方法赋值indeterminate模式为false */
		        progressBar.setIndeterminate(false);
				Toast.makeText(TBtnActivity.this, "结束", Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				  /* 调用setIndeterminate方法赋值indeterminate模式为false */
				progressBar.setVisibility(View.INVISIBLE);
			
				
				Toast.makeText(TBtnActivity.this, "开始", Toast.LENGTH_SHORT).show();
			}

			@Override
			protected void handleFailureMessage(Throwable e, String responseBody) {
				// TODO Auto-generated method stub
				super.handleFailureMessage(e, responseBody);
			}

			@Override
			protected void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
			}

			@Override
			protected void handleSuccessMessage(int statusCode,
					Header[] headers, String responseBody) {
				// TODO Auto-generated method stub
				super.handleSuccessMessage(statusCode, headers, responseBody);
			}

			@Override
			protected Message obtainMessage(int responseMessage, Object response) {
				// TODO Auto-generated method stub
				return super.obtainMessage(responseMessage, response);
			}

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					String content) {
				// TODO Auto-generated method stub
				super.onSuccess(statusCode, headers, content);
			}

			@Override
			protected void sendFailureMessage(Throwable e, byte[] responseBody) {
				// TODO Auto-generated method stub
				super.sendFailureMessage(e, responseBody);
			}

			@Override
			protected void sendFailureMessage(Throwable e, String responseBody) {
				// TODO Auto-generated method stub
				super.sendFailureMessage(e, responseBody);
			}

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

			@Override
			protected void sendMessage(Message msg) {
				// TODO Auto-generated method stub
				super.sendMessage(msg);
			}

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

			@Override
			protected void sendSuccessMessage(int statusCode, Header[] headers,
					String responseBody) {
				// TODO Auto-generated method stub
				super.sendSuccessMessage(statusCode, headers, responseBody);
			}
			
			
			
			
    		
    	
    	}
       );
    }
}

你可能感兴趣的:(13 Android AsyncHttpClient 使用)