2012-10-26 14 views
0

可能重複:
android.os.NetworkOnMainThreadException沒有得到JSON數據到MapView的,讓空白的地圖只有

我正在獲取JSON數據到MapView的展示位置,但每當我運行我的程序不獲取數據到地圖只是變得空白的地圖沒有JSON數據,在這裏我把我的JSON數據和活動代碼,請告訴我我在哪裏做錯 { 「地圖」:[ {

"title": "Place One", 
"latitude" : "46.483742", 
"longitude" : "7.663157", 
"country": "Switzerland" 
}, 
{ 
"title" : "Place Two", 
"latitude" : "59.25235", 
"longitude" : "18.465536", 
"country" : "Sweden" 
}, 
{ 
"title" : "Place Three", 
"latitude" : "56.404182", 
"longitude" : "-3.818855", 
"country" : "Scotland" 
} 
] 
} 

活動代碼: -

public class Main extends MapActivity { 
public GeoPoint point; 
TapControlledMapView mapView; // use the custom TapControlledMapView 
List<Overlay> mapOverlays; 
Drawable drawable; 
SimpleItemizedOverlay itemizedOverlay; 

@Override 
public void onCreate(final Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    mapView = (TapControlledMapView) findViewById(R.id.mapview); 
    mapView.setBuiltInZoomControls(true); 
    mapView.setSatellite(false); 
    // dismiss balloon upon single tap of MapView (iOS behavior) 
    mapView.setOnSingleTapListener(new OnSingleTapListener() {  
     public boolean onSingleTap(MotionEvent e) { 
      itemizedOverlay.hideAllBalloons(); 
      return true; 

     } 
    }); 

    mapOverlays = mapView.getOverlays();   
    drawable = getResources().getDrawable(R.drawable.ic_launcher); 
    itemizedOverlay = new SimpleItemizedOverlay 
      (drawable, mapView);   
    itemizedOverlay.setShowClose(false); 
    itemizedOverlay.setShowDisclosure(true); 
    itemizedOverlay.setSnapToCenter(false); 

    class DownloadWebPageTask extends AsyncTask<String, Void, String> { 
     @Override 
     protected String doInBackground(String... urls) { 

    HttpClient client = new DefaultHttpClient(); 
    // Perform a GET request for a JSON list 
    HttpUriRequest request = new HttpGet 
      ("https://dl.!!!!.com/maps.json"); 
    // Get the response that sends back 
    HttpResponse response = null; 
    try { 
     response = client.execute(request); 
    } catch (ClientProtocolException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } catch (IOException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 
    // Convert this response into a readable string 
    String jsonString = null; 
    try { 
     jsonString = StreamUtils.convertToString 
       (response.getEntity().getContent()); 
    } catch (IllegalStateException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } catch (IOException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 
    // Create a JSON object that we can use from the String 
    JSONObject json = null; 
    try { 
     json = new JSONObject(jsonString); 
    } catch (JSONException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 


       try{ 

    JSONArray jsonArray = json.getJSONArray("maps"); 
    Log.e("log_tag", "Opening JSON Array "); 
     for(int i=0;i < jsonArray.length();i++) 
      {      

      JSONObject jsonObject = jsonArray.getJSONObject(i); 

       String latitude = jsonObject.getString("latitude"); 
       String longitude = jsonObject.getString("longitude"); 
       String title = jsonObject.getString("title"); 
       String country = jsonObject.getString("country"); 


       double lat = Double.parseDouble(latitude); 
       double lng = Double.parseDouble(longitude); 


        Log.e("log_tag", "ADDING GEOPOINT"+title); 

         point = new GeoPoint(
          (int) (lat * 1E6), 
          (int) (lng * 1E6)); 
        OverlayItem overlayItem = new OverlayItem(point, title, 
          country); 
        itemizedOverlay.addOverlay(overlayItem); 

        } 
       }catch(JSONException e)  { 
       Log.e("log_tag", "Error parsing data "+e.toString()); 
      } 

       itemizedOverlay.populateNow(); 

       mapOverlays.add(itemizedOverlay); 
        if (savedInstanceState == null) { 
      MapController controller = mapView.getController(); 
         controller.setCenter(point); 
         controller.setZoom(7); 

        } else { 

       // example restoring focused state of overlays 
         int focused; 
       focused = savedInstanceState.getInt("focused_1", -1); 
      if (focused >= 0) { 
           itemizedOverlay.setFocus 
         (itemizedOverlay.getItem(focused)); 
         } 
        } 
        return jsonString; } 
    } 

    } 
+0

能否告訴我我在哪裏丟失 – Stanley

+1

@Stanley不要在主UI線程中編寫與網絡相關的任務線程,使用單獨的線程進行與網絡相關的任務。 –

+0

嗨Dipak,感謝您的回覆,但我很困惑,我需要寫我的代碼在哪裏,我可以爲我寫,請使用上面的代碼,這將是很容易理解我,我會進一步使用相同的方式。 .. – Stanley

回答

1

異常說,所有:您嘗試通過加載UI線程裏面的地圖做了網絡操作。這是一個壞主意,因爲網絡訪問速度很慢,並會在運行時阻止用戶界面。

您應該將地圖加載到不同的線程中,例如通過使用AsyncTask。 基本上你定義了一個擴展AsyncTask的類,然後運行doInBackground()中的下載並將結果填入onPostExecute()的視圖中。 有關更多信息,請參閱Android docs

你的情況,你需要把這個代碼塊到doInBackground()方法:

HttpClient client = new DefaultHttpClient(); 
// Perform a GET request for a JSON list 
HttpUriRequest request = new HttpGet("https://dl.###.com/maps.json"); 
// Get the response that sends back 
HttpResponse response = null; 
try { 
    response = client.execute(request); 
} catch (ClientProtocolException e1) { 
    // TODO Auto-generated catch block 
    e1.printStackTrace(); 
} catch (IOException e1) { 
    // TODO Auto-generated catch block 
    e1.printStackTrace(); 
} 

// Create a JSON object that we can use from the String 
JSONObject json = null; 
try { 
    json = new JSONObject(jsonString); 
} catch (JSONException e1) { 
    // TODO Auto-generated catch block 
    e1.printStackTrace(); 
} 

,並確保其內onPostExecute()

返回進一步處理 json對象(基本上是遵循這一行代碼)

查看示例文檔。您還可以查看this code以獲取更多示例。

+0

Heiko,您可以寫下您的想法,插入我的代碼,您認爲它應該在哪裏,因爲我不知道在哪裏以及在哪裏寫和寫.... – Stanley

+0

謝謝,我已經嘗試過,但我仍然面臨問題,請你爲我寫信,就像你說的那樣我會匹配我的代碼,並能夠找到原因或我的錯誤 – Stanley

+0

嗨Heiko,我已經嘗試了你的想法,現在我得到地圖,但沒有JSON數據,並且我沒有在postexecute()方法中寫任何代碼,因爲我沒有得到在這之下寫什麼,你能告訴我嗎? e早些時候您幫助 – Stanley