簡體   English   中英

如何使用一個Httppost請求檢索多個結果?

[英]How to retrieve multiple results with one Httppost request?

我正在嘗試使用httppost將數據從Android手機發送到php服務器。 連接正常。 在php端,我使用echo指定響應。 例:

<?php
if(isset($_REQUEST['data'])){
echo "This is the response.";
}
?>

如果我想將多個數據添加到同一請求中,這將檢索多個結果,而不僅僅是一個響應,這是棘手的事情。 這是我的Java代碼:

Httpclient hc = new DefaultHttpClient();
Httppost hp = new HttpPost("link");
list.add(new BasicNameValuePair("Name", "Mansour"));
list.add(new BasicNameValuePair("Age", "12"));
    try{
        hp.setEntity(new UrlEncodedFormEntity(list));
        hr = hc.execute(hp);
        BufferedReader br = new BufferedReader(new InputStreamReader(hr.getEntity().getContent()));
        StringBuffer sb = null;
        String compare;
        while((compare = br.readLine()) != null){
            sb.append(compare);
        }
        // Toast the response
        Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show();
    }catch(Exception e){
        e.printStackTrace();
    }       

您應該使用要發送的多個數據創建Object類,並將其序列化為json。 格森將是完美的。 https://sites.google.com/site/gson/gson-user-guide

希望它有用;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM