我如何使用post方法将数据从android发送到我们

时间:2012-11-06 21:14:19

标签: android post http-post

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

任何帮助..... !!

3 个答案:

答案 0 :(得分:2)

您可以创建ArrayList nameValuePairs并使用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);

答案 1 :(得分: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();
}

答案 2 :(得分:-1)

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

此处,当您需要设置正文时,您的key变量会显示一个包含帖子数据作为参数的网址。

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