HttpClient的POST方式请求的编写

package com.gyarmy.qqlogin;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

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.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.HttpParams;
import org.xmlpull.v1.XmlPullParser;

import com.gyarmy.qqlogin.utils.StreamTool;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	protected static final int SUCCESS = 0;
	protected static final int ERROR = 1;
	private EditText username;
	private EditText password;
	private TextView state;
	
	Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			
			switch (msg.what) {
				case SUCCESS:
					String data = (String)msg.obj;
					System.out.println(data);
					state.setText(data);
					state.setTextSize(25);
					break;
				case ERROR:
					Toast.makeText(MainActivity.this, "网络错误", 0);
					break;
	
				default:
					break;
			}
			
		}
		
		
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		username = (EditText)findViewById(R.id.qq_num);
		password = (EditText)findViewById(R.id.qq_pass);
		state = (TextView)findViewById(R.id.state);
	}
	
	public void login(View v){
		final String qq_num_str = username.getText().toString();
		final String qq_pass_str = password.getText().toString();
		
		//http://wx.cheshijie.com/qqlogin.php?name=admin&pass=123456
		//final String path = "http://www.xxxx.com/qqlogin_get.php?name="+qq_num_str+"&pass="+qq_pass_str;
		final String path = "http://www.xxx.com/qqlogin_post.php";
		
		new Thread(){
			public void run() {
				try {
					HttpClient client = new DefaultHttpClient();
					HttpPost post = new HttpPost(path);
					
					List<NameValuePair> parameters  = new ArrayList<NameValuePair>();
					parameters.add(new BasicNameValuePair("name", qq_num_str));
					parameters.add(new BasicNameValuePair("pass", qq_pass_str));
					
					post.setEntity(new UrlEncodedFormEntity(parameters, "utf-8"));
					
					HttpResponse response = client.execute(post);
					int code = response.getStatusLine().getStatusCode();
					
					
					if(200 == code){
						InputStream in = response.getEntity().getContent();
						String data = StreamTool.decodeData(in);
						
						Message msg = Message.obtain();
						msg.obj = data;
						msg.what = SUCCESS;
						mHandler.sendMessage(msg);
						
					}else{
						Message msg = Message.obtain();
						//msg.obj = data;
						msg.what = ERROR;
						mHandler.sendMessage(msg);
						
					}
					
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					
					Message msg = Message.obtain();
					//msg.obj = data;
					msg.what = ERROR;
					mHandler.sendMessage(msg);
				}
				
			};
			
		}.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;
	}

}

原文链接: HttpClient的POST方式请求的编写 版权所有,转载时请注明出处,违者必究。
注明出处格式:流沙团 ( https://gyarmy.com/post-124.html )

发表评论

0则评论给“HttpClient的POST方式请求的编写”