【问题标题】:How can i send data from android to we using post method我如何使用 post 方法将数据从 android 发送给我们
【发布时间】:2012-11-06 21:14:18
【问题描述】:

我想使用 post 方法从 android 设备发送一个简单的数据到 asp.net 页面.. 但我不知道如何从 android 请求网页!!!

asp页面很好,可以无错误地响应数据.. 但是android应用程序中的问题...

现在我正在使用此代码,但它不起作用

public void postData() throws ClientProtocolException, IOException, Exception {       
    String key = "https://www.itrack.somee.com/post.aspx?id=10&long=123&lat=123&alt=123";
    //URI  uri=new URI(key);

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(key);

    Toast.makeText(this, "here", Toast.LENGTH_LONG).show();
    HttpResponse response = httpclient.execute(httppost);
    Toast.makeText(this,"mm"+response.toString(), Toast.LENGTH_LONG).show();
}

任何帮助.....!!

【问题讨论】:

    标签: android post http-post


    【解决方案1】:

    您可以创建nameValuePairsArrayList 并使用setEntity 方法将它们附加到HttpPost。

    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("Var1",Var1_value));
    nameValuePairs.add(new BasicNameValuePair("Var2",Var2_value));
    //...ect
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("URL_HERE");       
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = httpclient.execute(httppost);
    

    【讨论】:

      【解决方案2】:

      我正在写这篇文章,因为 MrZander 的答案突然出现了……在@MrZander 的答案的基础上,这里有一个例子。如果可行,请将他的答案标记为已接受。

      public void postData() throws ClientProtocolException, IOException, Exception {       
          String key = "https://www.itrack.somee.com/post.aspx";
          //URI  uri=new URI(key);
      
          HttpClient httpclient = new DefaultHttpClient();
          HttpPost httppost = new HttpPost(key);
      
          List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
          nameValuePairs.add(new BasicNameValuePair("id", "10"));
          nameValuePairs.add(new BasicNameValuePair("long", "123"));
          nameValuePairs.add(new BasicNameValuePair("lat", "123"));
          nameValuePairs.add(new BasicNameValuePair("alt", "123"));
          httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
      
          Toast.makeText(this, "here", Toast.LENGTH_LONG).show();
          HttpResponse response = httpclient.execute(httppost);
          Toast.makeText(this,"mm"+response.toString(), Toast.LENGTH_LONG).show();
      }
      

      或者更好的是,我相信您可能正在寻找类似的东西?:

      public void postData(int id, double lat, double lng, double alt) throws ClientProtocolException, IOException, Exception {       
          String key = "https://www.itrack.somee.com/post.aspx";
          //URI  uri=new URI(key);
      
          HttpClient httpclient = new DefaultHttpClient();
          HttpPost httppost = new HttpPost(key);
      
          List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
          nameValuePairs.add(new BasicNameValuePair("id", "" + id));
          nameValuePairs.add(new BasicNameValuePair("long", String.valueOf(lat));
          nameValuePairs.add(new BasicNameValuePair("lat", String.valueOf(lng));
          nameValuePairs.add(new BasicNameValuePair("alt", String.valueOf(alt)));
          httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
      
          Toast.makeText(this, "here", Toast.LENGTH_LONG).show();
          HttpResponse response = httpclient.execute(httppost);
          Toast.makeText(this,"mm"+response.toString(), Toast.LENGTH_LONG).show();
      }
      

      【讨论】:

        【解决方案3】:

        根据我对网络服务的理解,post 方法要求 URL 参数在正文中发送,而不是作为 url 的一部分

        当您需要设置正文时,您的key 变量具有一个带有帖子数据作为参数的 url。

        get requests使用url参数 post requests 使用标题和正文

        【讨论】:

          猜你喜欢
          • 2010-12-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-07-19
          • 2012-09-10
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多