单击按钮上的Android HTTP发布请求

我想通过点击按钮发送HTTPpost请求到我的网站。 我搜索了allot只发现了这段代码

// Create a new HttpClient and Post Header HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php"); try { // Add your data List nameValuePairs = new ArrayList(2); nameValuePairs.add(new BasicNameValuePair("id", "12345")); nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!")); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); // Execute HTTP Post Request HttpResponse response = httpclient.execute(httppost); } catch (ClientProtocolException e) { // TODO Auto-generated catch block } catch (IOException e) { // TODO Auto-generated catch block } 

但我不知道点击按钮是如何工作的。

使用AsyncTask在按钮上执行网络操作单击为:

 public class onbuttonclickHttpPost extends AsyncTask { @Override protected String doInBackground(String... params) { byte[] result = null; String str = ""; // Create a new HttpClient and Post Header HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php"); try { // Add your data List nameValuePairs = new ArrayList(2); nameValuePairs.add(new BasicNameValuePair("id", "12345")); nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!")); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); // Execute HTTP Post Request HttpResponse response = httpclient.execute(httppost); StatusLine statusLine = response.getStatusLine(); if(statusLine.getStatusCode() == HttpURLConnection.HTTP_OK){ result = EntityUtils.toByteArray(response.getEntity()); str = new String(result, "UTF-8"); } } catch (ClientProtocolException e) { // TODO Auto-generated catch block } catch (IOException e) { // TODO Auto-generated catch block } return str; } /** * on getting result */ @Override protected void onPostExecute(String result) { // something with data retrieved from server in doInBackground } } 

并在按钮上单击启动AsyncTask onbuttonclickHttpPost为:

 buttonclick.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { new onbuttonclickHttpPost.execute(null); } }); 

试试这个…来自我的工作项目

我用ThreadNameValuePair来执行这个…..

 public String postData(String url, String xmlQuery) { final String urlStr = url; final String xmlStr = xmlQuery; final StringBuilder sb = new StringBuilder(); Thread t1 = new Thread(new Runnable() { public void run() { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(urlStr); try { List nameValuePairs = new ArrayList( 1); nameValuePairs.add(new BasicNameValuePair("xml", xmlStr)); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); Log.d("Vivek", response.toString()); HttpEntity entity = response.getEntity(); InputStream i = entity.getContent(); Log.d("Vivek", i.toString()); InputStreamReader isr = new InputStreamReader(i); BufferedReader br = new BufferedReader(isr); String s = null; while ((s = br.readLine()) != null) { Log.d("YumZing", s); sb.append(s); } Log.d("Check Now", sb + ""); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } /* * catch (ParserConfigurationException e) { // TODO * Auto-generated catch block e.printStackTrace(); } catch * (SAXException e) { // TODO Auto-generated catch block * e.printStackTrace(); } */ } }); t1.start(); try { t1.join(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("Getting from Post Data Method " + sb.toString()); return sb.toString(); }