android 使用post 提交

1、使用post 方式提交时不要把须要传递的參数写在URL 中,一定要使用 BasicNameValuePair 这个类来完毕

创建我想发送一个类似Get 方式的一个URL ---------- http://localhost/app/camera.php?

opt=discovery

在使用Post 方式提交的时候不要把后面的參数直接传递过去。一定要使用下面的方式

						HttpClient client = new DefaultHttpClient();
						HttpPost post = new HttpPost("http://localhost/app/camera.php");
						// 设置HTTP POST请求參数必须用NameValuePair对象 
						List<NameValuePair> params = new ArrayList<NameValuePair>();
						params.add(new BasicNameValuePair("opt", "discovery"));

						try {
							post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
							HttpResponse response = client.execute(post);
							if (response.getStatusLine().getStatusCode() == 200) {
								String string = EntityUtils.toString(response.getEntity());
								System.out.println(string);
								//Toast.makeText(MainActivity.this, response.getStatusLine().getStatusCode(), 1).show();

							} else {
								//Toast.makeText(MainActivity.this, response.getStatusLine().getStatusCode(), 1).show();

							}
						} catch (ClientProtocolException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}


posted on 2016-02-28 21:44  gcczhongduan  阅读(304)  评论(0编辑  收藏  举报