上一篇大概步骤都讲了,这一片会写的很简略哦。
网上说GET和POST的区别是:GET是向服务器索取数据的一种请求,而POST是向服务器提交数据的一种请求,要提交的数据位于信息头后面。
代码如下:
private class TwoThread implements Runnable {
private OutputStream os;
private InputStream is;
private BufferedReader br;
@Override
public void run() {
getPath();
try {
//将String类型的地址的封装为URL
url = new URL(todayWeather);
//打开服务器
conn = (HttpURLConnection) url.openConnection();
//设置请求方法为POST
conn.setRequestMethod("POST");
//POST方法不能缓存数据,则需要手动设置使用缓存的值为false
conn.setUseCaches(false);
//连接服务器
conn.connect();
/**写入参数*/
os = conn.getOutputStream();
//封装输出流
DataOutputStream dos = new DataOutputStream(os);
dos.writeBytes("name=" + URLEncoder.encode(edit.getText().toString()));
dos.close();
/**读取服务器数据*/
is = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
StringBuffer sb = new StringBuffer();
while ((line = br.readLine()) != null) {
sb.append(line);
}
sb.append("\n" + "\nWays:HttpUrlConnection-POST");
content = sb.toString();
Message message = new Message();
message.what = 1;
handler.sendEmptyMessage(message.what);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
if (br!=null){
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
又是子线程哦!!!
这个就是和上一篇唯一不同的地方了,我现在把源码贴出来自己看吧,因为好困,不想写了。
public class MainActivity extends Activity implements View.OnClickListener {
private EditText edit;
private Button one;
private Button two;
private Button three;
private Button four;
private TextView text;
private String todayWeather;
private Handler handler;
//HttpUrlConnection方式所需成员变量
private URL url;
private HttpURLConnection conn;
private String content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
one.setOnClickListener(this);
two.setOnClickListener(this);
three.setOnClickListener(this);
four.setOnClickListener(this);
}
private void initView() {
edit = (EditText) findViewById(R.id.edit);
one = (Button) findViewById(R.id.one);
two = (Button) findViewById(R.id.two);
three = (Button) findViewById(R.id.three);
four = (Button) findViewById(R.id.four);
text = (TextView) findViewById(R.id.text);
handler = new MyHandler();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.one:
new Thread(new OneThread()).start();
break;
case R.id.two:
//通过HttpUrlConnection的POST方式
new Thread(new TwoThread()).start();
break;
case R.id.three:
//通过HttpClient的GET方式
new Thread(new ThreeThread()).start();
break;
case R.id.four:
break;
default:
break;
}
}
public void getPath() {
String todayStart = "http://api.k780.com:88/?app=weather.today&weaid=";
String todayCity = edit.getText().toString();
String todayEnd = "&&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json";
todayWeather = (todayStart + todayCity + todayEnd);
}
class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
if (msg.what == 1) {
text.setText(content);
} else {
text.setText("出错了");
}
}
}
private class OneThread implements Runnable {
InputStream is;
BufferedReader br;
@Override
public void run() {
getPath();
try {
//通过路径得到URL对象
url = new URL(todayWeather);
//打开服务器
conn = (HttpURLConnection) url.openConnection();
//连接服务器
conn.connect();
//得到输入流
is = conn.getInputStream();
//封装
br = new BufferedReader(new InputStreamReader(is));
String line = null;
//将数据存储在StringBuffere中
StringBuffer sb = new StringBuffer();
while ((line = br.readLine()) != null) {
sb.append(line);
}
sb.append("\n" + "\nWays:HttpUrlConnection-GET");
content = sb.toString();
Message message = new Message();
message.what = 1;
handler.sendEmptyMessage(message.what);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
if (br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private class TwoThread implements Runnable {
private OutputStream os;
private InputStream is;
private BufferedReader br;
@Override
public void run() {
getPath();
try {
//将String类型的地址的封装为URL
url = new URL(todayWeather);
//打开服务器
conn = (HttpURLConnection) url.openConnection();
//设置请求方法为POST
conn.setRequestMethod("POST");
//POST方法不能缓存数据,则需要手动设置使用缓存的值为false
conn.setUseCaches(false);
//连接服务器
conn.connect();
/**写入参数*/
os = conn.getOutputStream();
//封装输出流
DataOutputStream dos = new DataOutputStream(os);
dos.writeBytes("name=" + URLEncoder.encode(edit.getText().toString()));
dos.close();
/**读取服务器数据*/
is = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
StringBuffer sb = new StringBuffer();
while ((line = br.readLine()) != null) {
sb.append(line);
}
sb.append("\n" + "\nWays:HttpUrlConnection-POST");
content = sb.toString();
Message message = new Message();
message.what = 1;
handler.sendEmptyMessage(message.what);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
if (br!=null){
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
one是表示点击使用HttpUrlConnection的GET方式的按钮;
two是表示点击使用HttpUrlConnection的POST方式的按钮;
three和four还没写呢,不要管。
好累,要去超市了,Bye!!!
这篇博客简要介绍了如何使用HttpUrlConnection在Android中进行POST请求。与GET请求的区别在于,POST请求用于向服务器提交数据,这些数据位于信息头后面。文章提供了相应的代码示例,并提到了该操作在子线程中执行。
8419

被折叠的 条评论
为什么被折叠?



