继续在monodroid应用程序中获取Java.IO.FileNotFoundException异常

时间:2013-09-30 10:45:35

标签: java android xamarin.android

我正在使用以下代码从Google下载数据并在我的monodroid应用程序中获取位置关键字:

public  LatLng  GetLatLng (String input)
    {
        HttpURLConnection conn = null;
        StringBuilder jsonResults = new StringBuilder ();
        LatLng gp = null;
        string str = null;
        try {
            StringBuilder sb = new StringBuilder (GEOCODER_API_BASE + OUT_JSON);
            sb.Append ("?address=" + input + "&sensor=true");
            /// "http://maps.googleapis.com/maps/api/geocode/json?address=Paris&sensor=true"
            URL url = new URL (sb.ToString ());
            conn = (HttpURLConnection)url.OpenConnection ();
            Java.IO.InputStreamReader inp = new Java.IO.InputStreamReader (conn.InputStream);

            // Load the results into a StringBuilder
            int read;
            char[] buff = new char[1024];
            while ((read = inp.Read (buff)) != -1) {
                jsonResults.Append (buff, 0, read);
            }
            str = jsonResults .ToString();



        } catch (System.IO.IOException e) {
            RltLog .HandleException (e, "\nError connecting to Places API\n");
            return gp;
        } finally {
            if (conn != null) {
                conn.Disconnect ();
            }
        }
        try {
            // Create a JSON object hierarchy from the results

            JObject address = JObject .Parse (str);            
            JArray ja = (JArray)address ["results"];

            JObject LocationJObejct = (JObject)ja [0] ["geometry"] ["location"];
            gp = HelperMethods .executeLocationPoint (LocationJObejct ["lat"].ToString (),
                                               LocationJObejct ["lng"].ToString ());


        } catch (Exception e) {
            RltLog .HandleException (e);
        }
        return gp;
    }

但我一直收到这些错误:

 Exception of type 'Java.IO.FileNotFoundException' was thrown. 

我想知道是什么原因?

1 个答案:

答案 0 :(得分:2)

您应该根据Google文档在请求中用+替换空格。