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 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://xxxx.xxx.com/qqlogin.php?name=admin&pass=123456
		final String path = "http://www.xxxx.com/qqlogin.php";
		new Thread(){
			public void run() {
				try {
					URL url = new URL(path);
					HttpURLConnection conn = (HttpURLConnection)url.openConnection();
					conn.setConnectTimeout(2000);
					conn.setRequestMethod("POST");
					conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
					String post_data="name="+qq_num_str+"&pass="+qq_pass_str;
					conn.setRequestProperty("Content-Length", post_data.length()+"");
					conn.setDoOutput(true);
					conn.getOutputStream().write(post_data.getBytes());
					
					int code = conn.getResponseCode();
					
					if(200 == code){
						InputStream in = conn.getInputStream();
						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;
	}

}

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

发表评论

0则评论给“post请求的编写”