C# 访问RESTWeb服务时,如何在android中使用HttpPost和NameValuePair传递参数值?

C# 访问RESTWeb服务时,如何在android中使用HttpPost和NameValuePair传递参数值?,c#,android,wcf,rest,C#,Android,Wcf,Rest,我使用服务合同创建了一个RESTWeb服务,如下所示 [OperationContract] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "postdataa?id={id}" )] string Pos

我使用服务合同创建了一个RESTWeb服务,如下所示

[OperationContract]
[WebInvoke(Method = "POST",
            ResponseFormat = WebMessageFormat.Xml,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "postdataa?id={id}"
            )]
string PostData(string id);
PostData方法的实现

public string PostData(string id)
        {
            return "You posted " + id;
        }
Android中用于在web服务中发布数据的代码

HttpClient httpclient = new DefaultHttpClient();
        HttpHost target = new HttpHost("192.168.1.4",4567);
        HttpPost httppost = new HttpPost("/RestService.svc/postdataa?");

        String result=null;
        HttpEntity entity = null;

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("id", "1"));
            UrlEncodedFormEntity ent = new UrlEncodedFormEntity(nameValuePairs);
            httppost.setEntity(ent);

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(target, httppost);
            entity = response.getEntity();
            //get xml result in string
            result = EntityUtils.toString(entity);

} catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
HttpClient-HttpClient=newdefaulthttpclient();
HttpHost目标=新的HttpHost(“192.168.1.4”,4567);
HttpPost-HttpPost=newhttppost(“/RestService.svc/postdataa?”);
字符串结果=null;
HttpEntity=null;
试一试{
//添加您的数据
List nameValuePairs=新的ArrayList(1);
添加(新的BasicNameValuePair(“id”,“1”));
UrlEncodedFormEntity=新的UrlEncodedFormEntity(nameValuePairs);
httppost.setEntity(ent);
//执行HTTP Post请求
HttpResponse response=httpclient.execute(目标,httppost);
entity=response.getEntity();
//获取字符串形式的xml结果
结果=EntityUtils.toString(实体);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
}捕获(IOE异常){
//TODO自动生成的捕捉块
}
问题在于xml结果显示,并且缺少参数值

<PostDataResponse xmlns="http://tempuri.org/"><PostDataResult>You posted </PostDataResult></PostDataResponse>
您发布了

我不知道出了什么问题

既然您正在使用REST服务,请尝试一下:

private static char[] GetData(String servicePath) throws Exception
{           
     InputStream stream = null;     
     String serviceURI = SERVICE_URI;//this is your URI to the service
     char[] buffer = null;
     try        
     {
        if (servicePath != "")
           serviceURI = serviceURI + servicePath; 
        DefaultHttpClient httpClient = new DefaultHttpClient();

        HttpGet request = new HttpGet(serviceURI);

        request.setHeader("Accept", "application/xml");
        request.setHeader("Content-type", "application/xml");

        HttpResponse response = httpClient.execute(request);

        HttpEntity responseEntity = response.getEntity();    
        if (responseEntity != null)
        {
            // Read response data into buffer
            buffer = new char[(int)responseEntity.getContentLength()];
            stream = responseEntity.getContent();
            InputStreamReader reader = new InputStreamReader(stream);
            reader.read(buffer);
            stream.close();  
        }
    }
    catch (Exception e)
    {
        Log.i("Survey Application", e.getMessage());
        throw e;
    }       
    return buffer;
}
使用调用此方法

try
{
     char[] buffer = GetData("postdataa/" + id);
     if (buffer != null)
          //Build your XML object
}
catch (Exception e)
{

}

Android代码看起来不错。。。您确定服务器正在接收ID吗?我感觉您混淆了rest声明中的get和post。我不太了解rest(请提供一个好的链接,我将不胜感激),但您正在将您的参数描述为uri的一部分(如在get方法中),并且希望使用post。我非常确定您的服务将使用url中传递的id工作。当我想要更新数据时,我使用post,因此我将数据传递给服务,并且我希望在传递参数值后看到服务的响应。根据我的研究,NameValuePair用于识别参数名称和值。