安卓开发--HttpClient

package com.zx.httpclient01;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;


public class MainActivity extends Activity {
	private Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        btn=(Button) findViewById(R.id.button1);
        btn.setOnClickListener(clickListener);
    }
    
    private android.view.View.OnClickListener clickListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO 自动生成的方法存根
			HttpClientThread httpClientThread = new HttpClientThread();
			httpClientThread.start();
		}
	};

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

  

package com.zx.httpclient01;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;



import android.util.Log;

public class HttpClientThread extends Thread {
	
	public void doHttpPost() {
		Log.i("tag", "1");
		HttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost("http://192.168.0.106:8088/User/AddUser");
		
		ArrayList<NameValuePair> list = new ArrayList<NameValuePair>();
		list.add(new BasicNameValuePair("loginname", "李四"));
		list.add(new BasicNameValuePair("loginpwd", "123456"));
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(list,"UTF-8"));
			HttpResponse response  = httpClient.execute(httpPost);
			Log.i("tag", String.valueOf(response.getStatusLine().getStatusCode()));
			if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
				String content = EntityUtils.toString(response.getEntity());
				Log.i("tag", content);
			}
			
		} catch (ClientProtocolException e) {
		// TODO 自动生成的 catch 块
		e.printStackTrace();
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		
	}

	public void doHttpGet() {
		HttpClient httpClient = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet("http://192.168.0.106:8088/Api/User/GetUserInfo?loginname=张三&loginpwd=abc@987");
		HttpResponse response = null;
		try {
			response = httpClient.execute(httpGet);
			if (response.getStatusLine().getStatusCode()==HttpStatus.SC_OK) {
				Log.i("tag", EntityUtils.toString(response.getEntity()));
			}
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}
	
	@Override
	public void run() {
		// TODO 自动生成的方法存根
		doHttpPost();
	}
}

  

posted @ 2015-12-27 20:02  在路上的白羊  阅读(350)  评论(0编辑  收藏  举报