如何在Web服务上发布HTTP帖子?

时间:2011-09-28 12:43:51

标签: java android eclipse http post

我需要对网络服务进行HTTP发布..

如果我把它放到像这样的网络浏览器中

http://server/ilwebservice.asmx/PlaceGPSCords?userid=99&longitude=-25.258&latitude=25.2548

然后将值存储到服务器上的数据库中..

在Eclipse中,使用Java编程实现android ..这个url看起来像

http://server/ilwebservice.asmx/PlaceGPSCords?userid="+uid+"&longitude="+lng1+"&latitude="+lat1+"

uidlng1lat1被指定为字符串..

我该如何运行?

由于

4 个答案:

答案 0 :(得分:5)

try {
    HttpClient client = new DefaultHttpClient();  
    String getURL = "http://server/ilwebservice.asmx/PlaceGPSCords?userid="+uid+"&longitude="+lng1+"&latitude="+lat1+";
    HttpGet get = new HttpGet(getURL);
    HttpResponse responseGet = client.execute(get);  
    HttpEntity resEntityGet = responseGet.getEntity();  
    if (resEntityGet != null) {  
                //do something with the response
                Log.i("GET RESPONSE",EntityUtils.toString(resEntityGet));
            }
} catch (Exception e) {
e.printStackTrace();
}

答案 1 :(得分:2)

对于http post使用名称值对。见下面的代码 -

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
    nameValuePairs.add(new BasicNameValuePair("longitude", long1));
    nameValuePairs.add(new BasicNameValuePair("latitude", lat1));
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePair));
    HttpResponse response = httpclient.execute(httppost);

答案 2 :(得分:2)

事实上,我已经完成了你将要做的事情。所以尝试这个stff。您可以创建一个这样的方法,并使用lat,long传递您的URL。这将返回HTTP connection.

的回复
public static int sendData(String url) throws IOException
    {
        try{
            urlobj = new URL(url);
            conn = urlobj.openConnection();
            httpconn= (HttpURLConnection)conn;
            httpconn.setConnectTimeout(5000);
            httpconn.setDoInput(true);
        }
        catch(Exception e){
            e.printStackTrace();}
        try{
            responseCode = httpconn.getResponseCode();}
        catch(Exception e){
            responseCode = -1;
            e.printStackTrace();
        }
        return responseCode;
    }

答案 3 :(得分:0)

我不确定我理解你的问题,但如果我这样做,我认为你可以使用java.net.UrlConnection:

URL url = new URL("http://server/ilwebservice.asmx/PlaceGPSCords?userid="+uid+"&longitude="+lng1+"&latitude="+lat1);
URLConnection conn = url.openConnection();
conn.connect();