将值从android传递给c#rest web服务并从那里检索值。

时间:2015-01-28 11:43:56

标签: android eclipse web-services wcf rest

在按钮上单击如何将值从android传递到c#rest web服务并从那里检索值。

我的Webserive就是这个

        [OperationContract]
    [WebInvoke(Method = "GET",
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "jsonnamesearch/{uid}")]
    List<PersonData> Jsonnamesearch(string uid);

和android onclick方法是这个

  public void onClick(View view) {
                        Log.d("aaa","nn");
                        String result=new String(" ");
                        //Intent intent = new Intent(getActivity(), CollectionDemoActivity.class);
                        //startActivity(intent);
                        EditText uname = (EditText)getActivity(). findViewById(R.id.Uname_search);  

                        String name=uname.getText().toString();

                        try {

                        HttpGet request = new HttpGet(SERVICE_URI+"/jsonnamesearch/"+URLEncoder.encode(name,"UTF-8")); 
                        Log.d("url",URLEncoder.encode(name,"UTF-8"));
                        request.setHeader("Accept", "application/json"); 
                        request.setHeader("Content-type", "application/json"); 
                        responseval = new String(" "); 
                        DefaultHttpClient httpClient = new DefaultHttpClient();
                        Log.d("request",request.toString()); 

                       HttpResponse response = httpClient.execute(request); 
                        Log.d("aaa3","name");
                        HttpEntity responseEntityAuth = response.getEntity();
                        Log.d("aaa4","name");
                        if (responseEntityAuth != null) 
                        {
                            Log.d("responseentity",responseEntityAuth.toString()); 
                            InputStream instream = responseEntityAuth.getContent();
                            result = convertStreamToString(instream); 
                            Log.d("result",result);
                            instream.close(); 
                        }
                        Log.d("response","received");
                         Log.d("WebInvoke", "Saving : " + response.getStatusLine().getStatusCode());
                       Toast.makeText(getActivity(), response.getStatusLine().getStatusCode() + "\n", Toast.LENGTH_LONG).show() ;

                        }catch (Exception e) {

                            Log.d("Exception",e.getMessage());
                        }
                        catch (Error e) {
                            Log.d("error",e.getMessage());
                        }

                    }

                }); 

很长一段时间,请任何人帮助我。

0 个答案:

没有答案