標籤:
參考文章:http://blog.csdn.net/lotusyangjun/article/details/22292445
http://blog.csdn.net/withiter/article/details/19908679
代碼如下:
package tdrhedu.com.edugame;import android.content.Intent;import android.graphics.Paint;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.UnsupportedEncodingException;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLEncoder;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;/** * Created by zhaoliang on 2016/6/15. */public class Login2Activity extends AppCompatActivity { private static final String TAG = "Login2Activity"; Button mLogin = null; TextView mForgetPassword = null; TextView mJustSee = null; EditText mAccountName, mAccountPasswd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login2); View v = findViewById(R.id.mLoginGuide);//找到你要設透明背景的layout 的id v.getBackground().setAlpha(70);//0~255透明度值 //TextView tv = (TextView)findViewById(R.id.find_password); //tv.setPaintFlags(tv.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); mLogin = (Button) findViewById(R.id.account_login); mLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d(TAG, "click"); String postReceiverUrl = "http://112.126.64.235:9090/company/get"; Log.v(TAG, "postURL: " + postReceiverUrl); new Thread(new Runnable(){ @Override public void run() { mAccountName = (EditText)findViewById(R.id.account_name); mAccountPasswd = (EditText)findViewById(R.id.account_password); String username = mAccountName.getText().toString(); String password = mAccountPasswd.getText().toString(); Map<String, String> params = new HashMap<String, String>(); params.put("username", username); params.put("password", password); String result = submitPostData(params, "utf-8"); Log.v(TAG, "result: " + result); } }).start(); //Intent i = new Intent(Login2Activity.this, PannelActivity.class); //startActivity(i); } }); /*mForgetPassword = (TextView) findViewById(R.id.find_password); mForgetPassword.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { Intent i = new Intent(Login2Activity.this, ForgotActivity.class); startActivity(i); } }); mJustSee = (TextView) findViewById(R.id.just_see); mJustSee.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } });*/ } /* * Function : 發送Post請求到伺服器 * Param : params請求體內容,encode編碼格式 * Author : [email protected] */ public static String submitPostData(Map<String, String> params, String encode) { byte[] data = getRequestData(params, encode).toString().getBytes();//獲得請求體 try { URL url = new URL("http://112.126.64.235:9090/company/get"); HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection(); httpURLConnection.setConnectTimeout(3000); //設定連線逾時時間 httpURLConnection.setDoInput(true); //開啟輸入資料流,以便從伺服器擷取資料 httpURLConnection.setDoOutput(true); //開啟輸出資料流,以便向伺服器提交資料 httpURLConnection.setRequestMethod("POST"); //設定以Post方式提交資料 httpURLConnection.setUseCaches(false); //使用Post方式不能使用緩衝 //佈建要求體的類型是文本類型 httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //佈建要求體的長度 httpURLConnection.setRequestProperty("Content-Length", String.valueOf(data.length)); //獲得輸出資料流,向伺服器寫入資料 OutputStream outputStream = httpURLConnection.getOutputStream(); outputStream.write(data); int response = httpURLConnection.getResponseCode(); //獲得伺服器的響應碼 if(response == HttpURLConnection.HTTP_OK) { InputStream inptStream = httpURLConnection.getInputStream(); return dealResponseResult(inptStream); //處理伺服器的響應結果 } } catch (IOException e) { e.printStackTrace(); } return ""; } /* * Function : 封裝請求體資訊 * Param : params請求體內容,encode編碼格式 * Author : [email protected] */ public static StringBuffer getRequestData(Map<String, String> params, String encode) { StringBuffer stringBuffer = new StringBuffer(); //儲存封裝好的請求體資訊 try { for(Map.Entry<String, String> entry : params.entrySet()) { stringBuffer.append(entry.getKey()) .append("=") .append(URLEncoder.encode(entry.getValue(), encode)) .append("&"); } stringBuffer.deleteCharAt(stringBuffer.length() - 1); //刪除最後的一個"&" } catch (Exception e) { e.printStackTrace(); } return stringBuffer; } /* * Function : 處理伺服器的響應結果(將輸入資料流轉化成字串) * Param : inputStream伺服器的響應輸入資料流 * Author : [email protected] */ public static String dealResponseResult(InputStream inputStream) { String resultData = null; //儲存處理結果 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] data = new byte[1024]; int len = 0; try { while((len = inputStream.read(data)) != -1) { byteArrayOutputStream.write(data, 0, len); } } catch (IOException e) { e.printStackTrace(); } resultData = new String(byteArrayOutputStream.toByteArray()); return resultData; }}
完畢。
android post請求