【问题标题】:Android communication with asp.net web serviceAndroid 与 asp.net 网络服务的通信
【发布时间】:2013-07-25 09:45:37
【问题描述】:

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

例如。 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
                }

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

【问题讨论】:

  • 您确定要发帖吗?看起来你可以做一个 get (即将 url 复制到浏览器中)。在这种情况下,您可以使用类似:new WebRequest(string.Format("http://yoors.somee.com/Default.aspx?type=EmailInsert&amp;username={0}&amp;password={1}&amp;name={2}&amp;email={3}&amp;emailenabled=false", userName, password, name, email) ).GetResponse();
  • 如果将response.getAllHeaders().toString() 替换为response.getStatusLine().getStatusCode() 会得到什么
  • @Skaard-Solo 我得到 200
  • 因此,您可以致电 WS。我认为更多的是关于参数。你能在执行之前获取httppost URL吗?您收到 FAIL 还是其他信息?
  • @Skaard-Solo 你能举个例子吗?

标签: android asp.net web-services rest


【解决方案1】:

试试这个,但我不是 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");

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-07
    • 1970-01-01
    • 1970-01-01
    • 2015-08-24
    • 2018-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多