douju9272 2013-05-17 14:59
浏览 101
已采纳

在服务器端未使用httppost发布

I am new to Android programming, but have decent knowledge of PHP.

I am trying to send data to my server via post request. I have the following code:

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("year","1980"));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream inps = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inps));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
    sb.append(line);
}
inps.close();
result=sb.toString();
Log.v("data-received",result);

I have found the code at numerous places all over the Internet.

On the PHP side I am writing this:

<?php
    echo "something".$_REQUEST['year'];
?>

But I am only getting "something" (in the Log value "data-received") as the output at my app end?

What am I doing wrong? Do I need to set any environment variable, etc.?

  • 写回答

4条回答 默认 最新

  • douxunzui1519 2013-05-22 15:45
    关注

    I finally got it to work. The only change is in the first line:

    List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePairs>();
    

    instead of

    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    

    Does anybody have any idea why is it working?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错