Android httpget适用于浏览器,但不适用于应用程序

时间:2014-04-29 05:55:14

标签: android google-maps http-get

我想从我的Android应用程序做一个httpGet,但我没有回应,当我从浏览器做同样的httpGet工作。我还使用chrome扩展名Postman来测试我发送的URL并且它可以正常工作

@Override
public void onLocationChanged(Location location) {
    // Draw the marker, if destination location is not set
    latitude = location.getLatitude();
    longitude = location.getLongitude();
    if (mMarkerPoints.size() < 2) {

        mLatitude = location.getLatitude();
        mLongitude = location.getLongitude();
        LatLng point = new LatLng(mLatitude, mLongitude);

        mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(point));
        mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(18));

        drawMarker(point);
    }
    String Text = "My current Latitude = " + latitude + " Longitude = "
            + longitude;
    Thread trd = new Thread(new Runnable() {
        @Override
        public void run() {
            try {

                SendQueryString();
            } catch (Throwable e) {
                e.printStackTrace();
                Log.i(".............", "Error");
            }
        }
    });
    trd.start();

}

public void SendQueryString() {

    String url = //
            "http://sistemamedicointegrado.azurewebsites.net/Home/Ubicacion?latitud="
            + latitude + "&longitud=" + longitude + "&id=1".toString().trim();
    try {
        HttpClient Client = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(url);
        Client.execute(httpget);
        Log.i(".............", "I am here"); 

    } catch (Exception ex) {

    }
}

3 个答案:

答案 0 :(得分:2)

错误在服务器端,来自控制器的操作方法不允许anonimus获取。

答案 1 :(得分:0)

使用AsyncTask并调用此方法

public int getResponceWithGet(String url) {
      int code = 0;

    try {
        HttpClient hc = new DefaultHttpClient();
        HttpGet get = new HttpGet(url);

        HttpResponse rp = hc.execute(get);


     code=  rp.getStatusLine().getStatusCode();
            return code;


    } catch (IOException e) {
        Log.e("calling service", e.toString());
        e.printStackTrace();
    }
    catch(Exception e)
    {
        Log.e("calling service", e.toString());
    }

    return code;
}

答案 2 :(得分:-1)

试试这段代码!!!

public void SendQueryString() {

    String url = //
            "http://sistemamedicointegrado.azurewebsites.net/Home/Ubicacion?latitud="
            + latitude + "&longitud=" + longitude + "&id=1".toString().trim();
    try {
        HttpClient Client = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(url);

        HttpResponse response = Client.execute(httpget);
        String result=response.toString();
         Log.i("Response", ":"+result); 
        Log.i(".............", "I am here"); 

    } catch (Exception ex) {

    }
}