Android与asp.net Web服务的通信

时间:2013-07-25 09:45:37

标签: android asp.net web-services rest

我正在尝试将数据发布到asp.net Web服务,但似乎我不知道该怎么做。所以我需要发布这样的数据

例如。 http://yoors.somee.com/Default.aspx?type=EmailInsert&username=MYUSERNAME&password=MYPASS&name=MYNAME&email=MYEMAIL@PROVIDER.com&emailenabled=FALSE

之后我得到像这样的JSON响应

{"VALID":"success"}

现在我正在使用这个功能,但似乎它不能用于此目的。 /

/ Create a new HttpClient and Post Header
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://yoors.somee.com/Default.aspx?type=EmailInsert");
                // http://yoors.somee.com/Default.aspx?type=Email
                // Insert&username=ben&password=pass&name=alv
                //&email=ben@yahoo.com&emailenabled=false
                try {
                    // Add your data
                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                    nameValuePairs.add(new BasicNameValuePair("username",userName));
                    nameValuePairs.add(new BasicNameValuePair("name", name));
                    nameValuePairs.add(new BasicNameValuePair("password", password));
                    nameValuePairs.add(new BasicNameValuePair("email", email));
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                    // Execute HTTP Post Request
                    HttpResponse response = httpclient.execute(httppost);
                    Toast.makeText(Signup.this, response.getAllHeaders().toString(), Toast.LENGTH_LONG).show();
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                }

所以任何人都可以指导我如何将数据发布到此Web服务

1 个答案:

答案 0 :(得分:0)

试试这个,但我不是asp.net专家:/

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://yoors.somee.com/Default.aspx?type=EmailInsert");
            try {
                // Add your data
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair("username",userName));
                nameValuePairs.add(new BasicNameValuePair("name", name));
                nameValuePairs.add(new BasicNameValuePair("password", password));
                nameValuePairs.add(new BasicNameValuePair("email", email));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                // Execute HTTP Post Request
                HttpResponse response = httpclient.execute(httppost);
                java.util.Scanner s = new java.util.Scanner(response.getEntity().getContent()).useDelimiter("\\A");
                responseString = s.hasNext() ? s.next() : "";
                Log.d("Debug", responseString);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
            } catch (IOException e) {
                // TODO Auto-generated catch block
            }

更新:

    HttpPost httppost = new HttpPost("http://yoors.somee.com/Default.aspx?type=EmailInsert&username=MYUSERNAME&password=MYPASS&name=MYNAME&email=MYEMAIL@PROVIDER.com&emailenabled=FALSE");

工作正常,但它并不是很漂亮......我认为有一些东西可以找到!