package org.tq.com;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class PostActivity extends Activity {
private Button but = null;
private EditText show = null;
private String baseUrl = "http://www.taaa.com/index.php";
HttpURLConnection urlConn = null;
URL url = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
but = (Button) this.findViewById(R.id.button2);
show = (EditText) this.findViewById(R.id.editText2);
but.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
url = new URL(baseUrl);
urlConn = (HttpURLConnection) url.openConnection();
urlConn.setDoInput(true); //设置输入流采用字节流
urlConn.setDoOutput(true); //设置输出流采用字节流
urlConn.setRequestMethod("POST");
urlConn.setUseCaches(false); //设置缓存
urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
urlConn.setRequestProperty("Charset", "utf-8");
urlConn.connect();
DataOutputStream dos = new DataOutputStream(urlConn.getOutputStream());
dos.writeBytes("tqpadmac="+URLEncoder.encode("B407F9D67C80", "utf-8"));
dos.writeBytes("tqpadver="+URLEncoder.encode("1", "utf-8"));
dos.flush();
dos.close();
//这句:new InputStreamReader(urlConn.getInputStream(), "gbk") 设置编码
BufferedReader br = new BufferedReader(new InputStreamReader(urlConn.getInputStream(),"gbk"));
String line = "";
String result = "";
while(null != (line=br.readLine())){
result += line;
}
System.out.println("接到的数据: " + result);
show.setText(result);
br.close();
urlConn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}