post 请求: package com.example.administrator.eschool; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.view.Vi
post 请求:
package com.example.administrator.eschool;import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText username;
private EditText password;
private Button login;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
textView.setText(msg.obj.toString());
}
};
private TextView textView;
private String text = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
login = (Button) findViewById(R.id.login);
login.setOnClickListener(this);
textView = (TextView) findViewById(R.id.textView);
textView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.login:
submit();
break;
}
}
private void submit() {
// validate
final String usernameString = username.getText().toString().trim();
if (TextUtils.isEmpty(usernameString)) {
Toast.makeText(this, "请输入您的用户名", Toast.LENGTH_SHORT).show();
return;
}
final String passwordString = password.getText().toString().trim();
if (TextUtils.isEmpty(passwordString)) {
Toast.makeText(this, "请输入您的密码", Toast.LENGTH_SHORT).show();
return;
}
// TODO validate success, do something
new Thread(new Runnable() {
@Override
public void run() {
try {
HttpURLConnection conn = (HttpURLConnection) new URL("http://192.168.1.107:8000/iphone_api/login.html").openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(5000);
conn.setDoOutput(true);
conn.setDoInput(true);
StringBuffer params = new StringBuffer();
// 表单参数与get形式一样
params.append("uid").append("=").append(usernameString).append("&")
.append("pwd").append("=").append(passwordString);
byte[] bypes = params.toString().getBytes();
conn.getOutputStream().write(bypes);// 输入参数
int code = conn.getResponseCode();
if (code == 200) {
InputStream is = conn.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
is.close();
text = new String(baos.toByteArray(), "utf-8");
Message m = new Message();
m.obj = text;
mHandler.sendMessage(m);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
}
【版权声明】本博文著作权归作者所有,任何形式的转载都请联系作者获取授权并注明出处!
【重要说明】本文为本人的学习记录,论点和观点仅代表个人而不代表当时技术的真理,目的是自我学习和有幸成为可以向他人分享的经验,因此有错误会虚心接受改正,但不代表此刻博文无误!
【Gitee地址】秦浩铖:https://gitee.com/wjw1014