2011-06-07 37 views
5

我正在製作一個帶有URL的應用程序。 * .asp擴展名,我們傳遞它所需的參數,並使用POST方法獲得一些字符串結果。從Android的互聯網鏈接獲取數據

有關如何實現此目的的任何建議?

更新:

其實我有一個.net鏈接,需要一些POST參數和給我一個結果。我怎麼能在Android中做到這一點?

+0

此URL正在加載中?你究竟在做什麼以及你想要實現什麼? – 2011-06-07 07:29:58

回答

4

類HTTPResponse應該做的伎倆:

DefaultHttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost("http://www.yoururl.com"); 

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); <!-- number should be the amount of parameters 
nameValuePairs.add(new BasicNameValuePair("nameOfParameter", "parameter")); 
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

HttpResponse response = httpclient.execute(httppost); 
HttpEntity ht = response.getEntity(); 

BufferedHttpEntity buf = new BufferedHttpEntity(ht); 

InputStream is = buf.getContent(); 

現在你有一個流與合作,將數據寫入到一個字符串:

BufferedReader r = new BufferedReader(new InputStreamReader(is)); 

total = new StringBuilder(); 
String line; 
while ((line = r.readLine()) != null) { 
    total.append(line); 
} 

祝你好運!

+0

我們該如何傳遞參數?爲此它會做一些東西,並返回結果?? – Shah 2011-06-07 07:50:19

+0

希望這有助於! – BadSkillz 2011-06-07 07:56:49

+0

完美。衷心感謝團隊 – Shah 2011-06-07 09:32:48

3

這裏是一個Android活動,做了POST請求(包括參數),並顯示在Android通知所接收的響應的代碼:

package org.anddev.android.webstuff; 

import java.io.ByteArrayInputStream; 
import java.io.UnsupportedEncodingException; 
import java.net.URLEncoder; 
import java.util.HashMap; 
import java.util.Iterator; 
import java.util.Map; 

import org.apache.http.util.ByteArrayBuffer; 

import android.app.Activity; 
import android.app.NotificationManager; 
import android.net.http.EventHandler; 
import android.net.http.Headers; 
import android.net.http.RequestQueue; 
import android.net.http.SslCertificate; 
import android.os.Bundle; 
import android.util.Log; 

public class HTTPPostExample extends Activity { 

     // =========================================================== 
     // Fields 
     // =========================================================== 

     private final String DEBUG_TAG = "httpPostExample"; 

     // =========================================================== 
     // 'Constructors' 
     // =========================================================== 

     @Override 
     public void onCreate(Bundle icicle) { 
       super.onCreate(icicle); 

       /* Create a new HTTP-RequestQueue. */ 
       android.net.http.RequestQueue rQueue = new RequestQueue(this); 

       /* Prepare the Post-Text we are going to send. */ 
       String POSTText = null; 
       try { 
         POSTText = "mydata=" + URLEncoder.encode("HELLO, ANDROID HTTPPostExample - by anddev.org", "UTF-8"); 
       } catch (UnsupportedEncodingException e) { 
         return; 
       } 
       /* And put the encoded bytes into an BAIS, 
       * where a function later can read bytes from. */ 
       byte[] POSTbytes = POSTText.getBytes(); 
       ByteArrayInputStream baos = new ByteArrayInputStream(POSTbytes); 

       /* Create a header-hashmap */ 
       Map<String, String> headers = new HashMap<String, String>(); 
       /* and put the Default-Encoding for html-forms to it. */ 
       headers.put("Content-Type", "application/x-www-form-urlencoded"); 

       /* Create a new EventHandler defined above, to handle what gets returned. */ 
       MyEventHandler myEvH = new MyEventHandler(this); 

       /* Now we call a php-file I prepared. It is exactly this: 
       * <?php 
       *    echo "POSTed data: '".$_POST['data']."'"; 
       * ?>*/ 
       rQueue.queueRequest("http://www.anddev.org/postresponse.php", "POST", 
           headers, myEvH, baos, POSTbytes.length,false); 

       /* Wait until the request is complete.*/ 
       rQueue.waitUntilComplete(); 
     } 

     // =========================================================== 
     // Worker Class 
     // =========================================================== 

     private class MyEventHandler implements EventHandler { 
       private static final int RANDOM_ID = 0x1337; 

       /** Will hold the data returned by the URLCall. */ 
       ByteArrayBuffer baf = new ByteArrayBuffer(20); 

       /** Needed, as we want to show the results as Notifications. */ 
       private Activity myActivity; 

       MyEventHandler(Activity activity) { 
         this.myActivity = activity; } 

       public void data(byte[] bytes, int len) { 
         baf.append(bytes, 0, len); } 

       public void endData() { 
         String text = new String(baf.toByteArray()); 
         myShowNotificationAndLog("Data loaded: n" + text); } 

       public void status(int arg0, int arg1, int arg2, String s) { 
         myShowNotificationAndLog("status [" + s + "]"); } 

       public void error(int i, String s) { 
         this.myShowNotificationAndLog("error [" + s + "]"); } 

       public void handleSslErrorRequest(int arg0, String arg1, SslCertificate arg2) { } 
       public void headers(Iterator arg0) { } 
       public void headers(Headers arg0) { } 

       private void myShowNotificationAndLog(String msg) { 
         /* Print msg to LogCat and show Notification. */ 
         Log.d(DEBUG_TAG, msg); 
         NotificationManager nm = (NotificationManager) this.myActivity 
             .getSystemService(Activity.NOTIFICATION_SERVICE); 
         nm.notifyWithText(RANDOM_ID, msg, NotificationManager.LENGTH_LONG, null); 
       } 
     } 
} 

代碼由plusminus

0

This link看起來很有幫助。我還沒有看完它,但它看起來正是OP正在尋找的東西(以及我自己)。