1

我想使用 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();
}

任何帮助......!

4

3 回答 3

2

您可以使用该方法创建一个ArrayListofnameValuePairs并将它们附加到 HttpPost setEntity

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);
于 2012-11-06T21:25:49.363 回答
0

我正在写这篇文章,因为 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();
}
于 2012-11-06T21:31:19.473 回答
-1

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

在这里key,当您需要设置正文时,您的变量以带有帖子数据作为参数的 url 为特色。

get requests使用 url 参数 post requests使用 headers 和 body

于 2012-11-06T21:17:34.970 回答