我正在尝试使用 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();
}