安卓开发--HttpDemo02

package com.cnn.httpdemo02;

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


public class MainActivity extends Activity {
	private EditText txtLoginName;
	private EditText txtLoginpwd;
	private Button btnLogin;
	private String httpURL;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        httpURL="http://192.168.0.106:8088/Api/User/PostUserInfo";
        txtLoginName = (EditText) findViewById(R.id.txtLoginName);
        txtLoginpwd = (EditText) findViewById(R.id.txtLoginPwd);
        btnLogin = (Button) findViewById(R.id.btnLogin);
        
        btnLogin.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO 自动生成的方法存根
				new UserThread(txtLoginName.getText().toString(), txtLoginpwd.getText().toString(), httpURL).start();
			}
		});
    }


}

  

package com.cnn.httpdemo02;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;

import android.util.Log;

public class UserThread extends Thread {
	private String LoginName,LoginPwd,HttpURL;
	
	public UserThread(String LoginName,String LoginPwd, String HttpURL){
		this.LoginName=LoginName;
		this.LoginPwd=LoginPwd;
		this.HttpURL=HttpURL;
	}
	
	private void GetUserInfo(){
		try {
			HttpURL=HttpURL+"?loginname="+URLEncoder.encode(LoginName,"utf-8")+"&loginpwd="+LoginPwd;
			URL httpURL=new URL(HttpURL);
			HttpURLConnection conn=(HttpURLConnection) httpURL.openConnection();
			conn.setReadTimeout(5000);
			conn.setRequestMethod("GET");
			BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			String str;
			StringBuffer buffer=new StringBuffer();
			while ((str=reader.readLine())!=null) {
				buffer.append(str);
				//Log.i("测试", str);				
			}
			
			//System.out.print(buffer.toString());
			Log.i("tag", buffer.toString());
		} catch (MalformedURLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}
	
	private void PostUserInfo() {
		try {
			URL httpURL=new URL(HttpURL);
			HttpURLConnection connection=(HttpURLConnection) httpURL.openConnection();
			connection.setReadTimeout(5000);
			connection.setRequestMethod("POST");
			connection.setDoOutput(true);
			OutputStream outputStream = connection.getOutputStream();
			String content = "loginname="+LoginName+"&loginpwd="+LoginPwd+"&a=29";
			outputStream.write(content.getBytes());
	
			BufferedReader reader=new BufferedReader(new InputStreamReader(connection.getInputStream()));
			String str;
			StringBuffer buffer=new StringBuffer();
			while ((str=reader.readLine())!=null) {
				buffer.append(str);
			}
			
			Log.i("tag", buffer.toString());
		} catch (MalformedURLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}
	
	@Override
	public void run() {
		// TODO 自动生成的方法存根
		//GetUserInfo();
		PostUserInfo();
	}
}

  

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.cnn.httpdemo02.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="38dp"
        android:layout_marginTop="15dp"
        android:text="用户名" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="15dp"
        android:text="密码" />

    <EditText
        android:id="@+id/txtLoginName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_marginLeft="34dp"
        android:layout_toRightOf="@+id/textView1"
        android:hint="请输入您的用户名"
        android:ems="10" >

        <requestFocus />
    </EditText>
    
        <EditText
        android:id="@+id/txtLoginPwd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_marginLeft="34dp"
        android:layout_toRightOf="@+id/textView2"
        android:hint="请输入您的密码"
        android:ems="10" >

        <requestFocus />
    </EditText>

        <Button
            android:id="@+id/btnLogin"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/txtLoginPwd"
            android:layout_below="@+id/txtLoginPwd"
            android:layout_marginTop="56dp"
            android:text="登陆" />

</RelativeLayout>

  

原文地址:https://www.cnblogs.com/zxcnn/p/5080705.html