2017-03-29 14 views
0

URLからマーカーを取得しようとしています。私が望むのは、すべてのマーカーをthis web serviceから得ることです。asynctaskを使用してURLからGoogleマップマーカーを取得するには?

私のコードは機能していません。 Webサービスからmap_iconを取得していますが、マップには表示されません。それを機能させるために私は何ができますか?

ログメッセージ:

E/pj: http://towncitycards.com/wp-content/uploads/2015/01/googlemapicons/5percent.png 
W/art: Suspending all threads took: 78.773ms 
E/addMarkers: 0 
W/System.err: android.os.NetworkOnMainThreadException 
W/System.err:  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303) 
W/System.err:  at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:86) 
W/System.err:  at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74) 
W/System.err:  at java.net.InetAddress.getAllByName(InetAddress.java:752) 
W/System.err:  at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29) 
W/System.err:  at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:187) 
W/System.err:  at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:156) 
W/System.err:  at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:98) 
W/System.err:  at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:345) 
W/System.err:  at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:328) 
W/System.err:  at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:246) 
W/System.err:  at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:457) 
W/System.err:  at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:405) 
W/System.err:  at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:243) 
W/System.err:  at app.towncitycards.activities.Frnt_mapActivity.addMarkers(Frnt_mapActivity.java:299) 
W/System.err:  at app.towncitycards.activities.Frnt_mapActivity.access$300(Frnt_mapActivity.java:73) 
W/System.err:  at app.towncitycards.activities.Frnt_mapActivity$Frnt_mIcons_Activity.onPostExecute(Frnt_mapActivity.java:428) 
W/System.err:  at app.towncitycards.activities.Frnt_mapActivity$Frnt_mIcons_Activity.onPostExecute(Frnt_mapActivity.java:332) 
W/System.err:  at android.os.AsyncTask.finish(AsyncTask.java:667) 
W/System.err:  at android.os.AsyncTask.-wrap1(AsyncTask.java) 
W/System.err:  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:684) 
W/System.err:  at android.os.Handler.dispatchMessage(Handler.java:102) 
W/System.err:  at android.os.Looper.loop(Looper.java:154) 
W/System.err:  at android.app.ActivityThread.main(ActivityThread.java:6119) 
W/System.err:  at java.lang.reflect.Method.invoke(Native Method) 
W/System.err:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
W/System.err:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
E/pj: http://towncitycards.com/wp-content/uploads/2015/01/googlemapicons/save£2.png 
E/addMarkers: 1 
W/System.err: android.os.NetworkOnMainThreadException 
W/System.err:  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303) 
W/System.err:  at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:86) 
W/System.err:  at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74) 
W/System.err:  at java.net.InetAddress.getAllByName(InetAddress.java:752) 
W/System.err:  at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29) 
W/System.err:  at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:187) 
W/System.err:  at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:156) 
W/System.err:  at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:98) 
W/System.err:  at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:345) 
W/System.err:  at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:328) 
W/System.err:  at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:246) 
W/System.err:  at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:457) 
W/System.err:  at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:405) 
W/System.err:  at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:243) 
W/System.err:  at app.towncitycards.activities.Frnt_mapActivity.addMarkers(Frnt_mapActivity.java:299) 
W/System.err:  at app.towncitycards.activities.Frnt_mapActivity.access$300(Frnt_mapActivity.java:73) 
W/System.err:  at app.towncitycards.activities.Frnt_mapActivity$Frnt_mIcons_Activity.onPostExecute(Frnt_mapActivity.java:428) 
W/System.err:  at app.towncitycards.activities.Frnt_mapActivity$Frnt_mIcons_Activity.onPostExecute(Frnt_mapActivity.java:332) 
W/System.err:  at android.os.AsyncTask.finish(AsyncTask.java:667) 
W/System.err:  at android.os.AsyncTask.-wrap1(AsyncTask.java) 
W/System.err:  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:684) 
W/System.err:  at android.os.Handler.dispatchMessage(Handler.java:102) 
W/System.err:  at android.os.Looper.loop(Looper.java:154) 
W/System.err:  at android.app.ActivityThread.main(ActivityThread.java:6119) 
W/System.err:  at java.lang.reflect.Method.invoke(Native Method) 
W/System.err:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
W/System.err:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

マイコード

public class Frnt_mapActivity extends Activity { 

public String Shop_title; 
public String Shop_address; 
public String Shop_icons; 
ProgressDialog mDialog; 
JSONObject jsonobject3; 
JSONArray jsonarray3; 
// Google Map 
private static GoogleMap googlemap; 

ArrayList<SearchBeams>getAllimgs; 
// Location 
ArrayList<String> world_locationlist; 
ArrayList<LocationBeams> worldlocation; 
// Looking for 
ArrayList<String> looking_forlist; 
ArrayList<LookingForBeams> world_looking_for; 

public static ArrayList<SearchBeams> searchdata_list; 
public static ArrayList<NormalSearchBeams> normlSearchList; 
public static String mid; 
public static String mpost_title; 
public static String mimage;  
public static List<String> imglist; 

ArrayList<All_products_lat_long> venueList; 

RelativeLayout r1; 
public static ArrayList<SearchBeams> GetIMG; 
public static TextView txtlocation; 
public static TextView txtlookingfor; 

public static ArrayList<All_products_lat_long> mpItm; 
String vals; 


private int i; 
private Intent intent; 
HashMap<Marker, Integer> hashMap=new HashMap<Marker, Integer>(); 
public static LatLng latlong ; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.frnt_map_activity); 

    googlemap=((MapFragment)getFragmentManager().findFragmentById(R.id.places_map)).getMap(); 
    googlemap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
    googlemap.setMyLocationEnabled(true); 
    googlemap.getUiSettings().setZoomControlsEnabled(true); 
    googlemap.getUiSettings().setMyLocationButtonEnabled(true); 
    googlemap.getUiSettings().setCompassEnabled(true); 
    googlemap.getUiSettings().setRotateGesturesEnabled(true); 
    googlemap.getUiSettings().setZoomGesturesEnabled(true); 
    googlemap.setMyLocationEnabled(true); 
    intent=new Intent(Frnt_mapActivity.this,MainActivity.class); 
    googlemap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() { 

     @Override 
     public void onInfoWindowClick(Marker arg0) { 
      i=hashMap.get(arg0); 
       intent.putExtra("PRODUCT_ID", venueList.get(i).getId()); 
       startActivity(intent); 
       //overridePendingTransition(R.anim.slide_right, R.anim.slide_left); 
     } 
    }); 


    new Frnt_mIcons_Activity().execute(); 


} 
private void addMarkers() { 

     for(int k=0; k<venueList.size(); k += 1){ 

      //nirmal 
      try{ 
       Log.e("pj",venueList.get(k).getMap_icon()); 

       //new AddMarkerAsyncTask(venueList.get(k),k).execute(); 

       double ln = Double.parseDouble(venueList.get(k).getLongitude()); 
       double la = Double.parseDouble(venueList.get(k).getLatitude()); 
       String title=venueList.get(k).getPost_title()==null?"":venueList.get(k).getPost_title(); 
       URL url = new URL(venueList.get(k).getMap_icon()); 
       Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream()); 
       BitmapDescriptor bd=BitmapDescriptorFactory.fromBitmap(image); 

       Marker marker=googlemap.addMarker(new MarkerOptions() 
         .position(new LatLng(la, ln)) 
         .title(title) 
         .icon(bd)); 
       hashMap.put(marker, k); 

      }catch (Exception e){ 
       Log.e("addMarkers",k+""); 
       e.printStackTrace(); 
      } 


     } 
     } 

/******************FOR MAP******************/ 
class Frnt_mIcons_Activity extends AsyncTask<String, String, String> { 

    public ArrayList<All_products_lat_long> maplist; 


    @Override 
    protected void onPreExecute() { 
     mDialog = new ProgressDialog(Frnt_mapActivity.this); 
     mDialog.setMessage("Loading..."); 
     mDialog.setCancelable(false); 
     mDialog.show(); 
     super.onPreExecute(); 
    } 




    // Slow Implementation 
    private String inputStreamToString(InputStream is) { 
     String s = ""; 
     String line = ""; 
     // Wrap a BufferedReader around the InputStream 
     BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 
     // Read response until the end 
     try { 
      while ((line = rd.readLine()) != null) { 
       s += line; 
       } 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     // Return full string 
     return s; 
    } 


    @Override 
    protected String doInBackground(String... params) { 
     // TODO Auto-generated method stub 
     maplist = new ArrayList<All_products_lat_long>(); 

     HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER; 

     DefaultHttpClient client = new DefaultHttpClient(); 

     SchemeRegistry registry = new SchemeRegistry(); 
     SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory(); 
     socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier); 
     registry.register(new Scheme("http", socketFactory, 443)); 
     SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry); 
     DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams()); 

// Set verifier 
      HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier); 


     HttpPost httpPost = new HttpPost("http://towncitycards.com/webservice_action.php?action=all_products"); 

     try{ 

      HttpResponse response = httpClient.execute(httpPost); 
      String data = inputStreamToString(response.getEntity().getContent()); 
      jsonobject3 = new JSONObject(data); 
      jsonarray3 = new JSONArray(jsonobject3.getString("all_products")); 
      venueList =new ArrayList<All_products_lat_long>(); 
      for(int j=0; j<jsonarray3.length();j++){ 
       JSONObject itemobj = jsonarray3.getJSONObject(j); 
       //SHOW SEARCH RESULT 
       All_products_lat_long searchItems = new All_products_lat_long(); 

       searchItems.setId(itemobj.getString("ID")); 
       searchItems.setPost_title(itemobj.getString("post_title")); 
       searchItems.setMap_icon(itemobj.getString("map_icon")); 
       searchItems.setLongitude(itemobj.getString("longitude")); 
       searchItems.setLatitude(itemobj.getString("latitude")); 
    //   System.out.println("######Ashish is object >>"+itemobj); 
       venueList.add(searchItems); 
    //  System.out.println("[email protected]@@@@@@"+venueList.get(j).getLongitude()); 
      } 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
     return null; 
    } 


    @Override 
    public void onPostExecute(String result) { 
     super.onPostExecute(result); 

     //System.out.println("[email protected]@@@@@@"+venueList); 
     //System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAA"+venueList); 
     try{ 
      if(venueList.size()>0) 
      { 
      addMarkers(); 
     //Toast.makeText(Frnt_mapActivity.this,"Search for the venue11.",Toast.LENGTH_SHORT).show(); 

      } 
      else 
      Toast.makeText(Frnt_mapActivity.this,"Search for the venue.",Toast.LENGTH_SHORT).show(); 
      //LatLng coordinate = new LatLng(22.75852160, 75.8911550); 
      LatLng coordinate = new LatLng(50.85514, 0.58382); 
      CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 18); 
      googlemap.animateCamera(yourLocation); 
      for (int i = 0; i < jsonarray3.length(); i++) { 

       jsonobject3 = jsonarray3.getJSONObject(i); 

       Shop_title = jsonobject3.optString("post_title"); 
       Shop_address = jsonobject3.optString("map_icon"); 
       Shop_icons = jsonobject3.optString("map_icon");  
         //} 
      } 


     }catch(Exception e){ 
      e.printStackTrace(); 
     } 

     if(mDialog!=null && mDialog.isShowing()){ 
      mDialog.dismiss(); 
     } 
     } 
    } 

} 
+0

は、要求がブロックされているように、私は –

答えて

0

NetworkOnMainThreadExceptionアプリケーションがメインスレッドにネットワーク操作を実行しようとしたときにスローされる例外。中

あなたonPostExecute()addMarkers()方法でマーカー画像をロードし、ビットマップを使用するようにpicassoを使用doInBackground

にあなたのJSONの構文解析コードを移動します。

private Target target = new Target() { 
     @Override 
     public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { 
      //here you get the image bitmap 
      //do your work 
     } 

     @Override 
     public void onBitmapFailed(Drawable errorDrawable) { 
     } 

     @Override 
     public void onPrepareLoad(Drawable placeHolderDrawable) { 
     } 
} 

呼び出し、それを使用して:

Picasso.with(this).load("your_url").into(target); 

Gradleの追加に:compile 'com.squareup.picasso:picasso:2.5.2'

+0

に探してお勧めらしいですこれをPicasso.with(this))load( "your_url")。into(target); 'for theループといいます。あなたはすでにそれを使用しています。あなたの 'addMarkers()'関数でネットワーク呼び出しを削除し、ピカソ – priya

+0

を何ができるか、複数のURL用のAndroidのセキュリティポリシー – rafsanahmad007

関連する問題