简体   繁体   English

访问其他Web服务时如何在android中使用HttpPost和NameValuePair传递参数值?

[英]How to pass parameter value using HttpPost and NameValuePair in android when accessing rest web service?

I made a rest web service with the service contract as shown below 我使用服务合同制作了一个休息Web服务,如下所示

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

Implementation of the method PostData PostData方法的实现

public string PostData(string id)
        {
            return "You posted " + id;
        }

Code in Android to post data in web service 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
        }

The problem is that the xml result shows and the value of the parameter is missing : 问题是xml结果显示并且缺少参数的值

<PostDataResponse xmlns="http://tempuri.org/"><PostDataResult>You posted </PostDataResult></PostDataResponse>

I do not know what went wrong. 我不知道出了什么问题。

Since you are using REST service, give this a try: 由于您使用的是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;
}

Invoke this method using 使用调用此方法

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM