【问题标题】:JSON Parsing runtime error source not found in android在android中找不到JSON解析运行时错误源
【发布时间】:2013-12-07 10:42:41
【问题描述】:

我正在尝试进行 json 解析以获取城市状态和邮政编码。 我正在为此使用 google api。

我在两个设备 android 2.3 和 android 4.1.2 上调试以下代码。 在 android 2.3 中它工作正常。但在 android 4.1.2 中我得到运行时错误。

这是我的代码

String url = "http://maps.google.com/maps/api/geocode/json?address="+address+"&sensor=true";
JSONParser jparser = new JSONParser();
JSONObject jobject = jparser.getJSONFromUrl(url);



try {
      DefaultHttpClient httpClient = new DefaultHttpClient();
      HttpPost httpPost= new HttpPost(url);
      HttpResponse httpResponse = httpClient.execute(httpPost);
      HttpEntity httpEntity = httpResponse.getEntity();
      is = httpEntity.getContent();
}catch (UnsupportedEncodingException e) {
      e.printStackTrace();
} catch (ClientProtocolException e) {
      e.printStackTrace();
} catch (IOException e) {
     e.printStackTrace();
}

HttpResponse httpResponse = httpClient.execute(httpPost); 行上,我收到一个运行时错误 Source not fount from ActivityThread.performLaunchActivity。 请解决我的问题

【问题讨论】:

    标签: java android json google-maps


    【解决方案1】:

    我也面临同样的问题。 这个可以帮助你。在AsyncTaskdoInBackground方法中使用下面的函数并检查响应然后你可以在onPostExecute方法中解析它

      public static String getResponefromURL(String url) {
                InputStream is = null;
                String result = "";
    
                // http post
                try {
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost(url.trim());
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();
                    is = entity.getContent();
    
                } catch (Exception e) {
                    Log.e("log_tag", "Error in http connection " + e.toString());
                }
    
                // convert response to string
                try {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
                    StringBuilder sb = new StringBuilder();
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                    is.close();
                    result = sb.toString();
                } catch (Exception e) {
                    Log.e("log_tag", "Error converting result " + e.toString());
                }
                return result;
            }
    

    【讨论】:

      【解决方案2】:

      请参考https://stackoverflow.com/a/18259495/1944782,附加url参数并根据您的要求制作url。完成此操作后,请将您的代码放在 AsyncTask 后台并从您的主 Activity 调用 AsyncTask。希望这样做后您的问题会得到解决。

      更新

      请参考以下代码:

      public JSONObject getAddressData() {
      
      HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?address="+address+"&sensor=true");
      HttpClient client = new DefaultHttpClient();
      HttpResponse response;
      StringBuilder stringBuilder = new StringBuilder();
      
      try {
          response = client.execute(httpGet);
          HttpEntity entity = response.getEntity();
          InputStream stream = entity.getContent();
          int b;
          while ((b = stream.read()) != -1) {
              stringBuilder.append((char) b);
          }
      } catch (ClientProtocolException e) {
          } catch (IOException e) {
      }
      
      JSONObject jsonObject = new JSONObject();
      try {
          jsonObject = new JSONObject(stringBuilder.toString());
      } catch (JSONException e) {
          e.printStackTrace();
      }
      return jsonObject;
      

      }

      【讨论】:

      • 我照你说的做了,但我还是在同一个地方遇到了同样的错误。
      【解决方案3】:

      我希望这对您有所帮助,您可以使用此代码获得确切的地址位置

      public String gpsadd() {
          try {
      
      
          if(isGPS) {
      
      
                      Geocoder gc = new Geocoder(Home.this, Locale.getDefault());
               try 
                {
                  addflg=0;
                  lat=location.getLatitude();
                  lng=location.getLongitude();
                  boolean lop=true;
                  while(lop) {
                  List<Address> addresses = gc.getFromLocation(lat, lng, 1);
                  if (addresses.size() > 0) 
                  {
                      lop=false;
                      addflg=1;
                      address = addresses.get(0);
                      s_addr=address.getAddressLine(0)+" "+address.getAddressLine(1)+" "+address.getAddressLine(2);
                      disp="Location\n "+address.getAddressLine(0)+"\n"+address.getAddressLine(1)+"\n"+address.getAddressLine(2);
      
                  }
      
                  }
      
      
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
          } else {
              display("GPS not Enabled");
      
          }
          } catch(Exception e) {
              e.printStackTrace();
      
          }
      
          return disp;
      }
      

      【讨论】:

        猜你喜欢
        • 2018-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多