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条)

报告相同问题?