2017-10-13 29 views
-2

我有這段代碼,我不知道如何將其更改爲RxJava 2.此代碼幫助我從Google API獲取JSON並獲取位置(經度和緯度)。請幫助我:如何將AsyncTask更改爲RxJava

class DownloadLocal extends AsyncTask<String,Void,String> { 

    @Override 
    protected String doInBackground(String... strings) { 
     StringBuilder stringBuilder = new StringBuilder(); 
     try { 
      URL url = new URL(strings[0]); 
      HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 
      httpURLConnection.connect(); 

      InputStream inputStream = httpURLConnection.getInputStream(); 
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 

      String line; 

      while ((line = bufferedReader.readLine()) != null){ 
       stringBuilder.append(line+"\n"); 
      } 



     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return stringBuilder.toString(); 
    } 

    @Override 
    protected void onPostExecute(String s) { 
     super.onPostExecute(s); 
     String address = ""; 
     try { 
      JSONObject jsonObject = new JSONObject(s); 
      JSONArray results = jsonObject.getJSONArray("results"); 
      for (int i =0 ;i<results.length();i++){ 
       JSONObject object = results.getJSONObject(i); 
       address = object.getString("formatted_address"); 
       JSONObject geometry = object.getJSONObject("geometry"); 
       JSONObject location = geometry.getJSONObject("location"); 
       latitude = (double) location.get("lat"); 
       longitude = (double) location.get("lng"); 
      } 
      if(address == null){ 
       Toast.makeText(getActivity(),"Can't get Address !!! Try Again ...", Toast.LENGTH_SHORT).show(); 
       return; 
      } 
      else{ 
       googleMap.clear(); 
       String link = "https://maps.googleapis.com/maps/api/directions/json?origin=" + locationNow.getLatitude() + "," + locationNow.getLongitude() 
         + "&destination=" + latitude+ "," + longitude +"&key=APIKEY"; 
       LatLng latLng = new LatLng(locationNow.getLatitude(),locationNow.getLongitude()); 
       MarkerOptions markerOptions = new MarkerOptions(); 
       markerOptions.position(latLng); 
       markerOptions.title("Me"); 

       googleMap.addMarker(markerOptions); 
       Location locationPoint = new Location(""); 

       locationPoint.setLatitude(latitude); 
       locationPoint.setLongitude(longitude); 
       double distance = locationNow.distanceTo(locationPoint)/1000; 
       txtdistance.setText(String.format("%.3f",distance)+" km"); 
       LatLng latLngPoint = new LatLng(latitude,longitude); 
       MarkerOptions markerOptionsPoint = new MarkerOptions(); 
       markerOptionsPoint.position(latLngPoint); 
       markerOptionsPoint.title("Point"); 
       googleMap.addMarker(markerOptionsPoint); 
       CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLngPoint,14); 
       googleMap.moveCamera(cameraUpdate); 
       mapPresenter.DislayStreet(googleMap,link); 
      } 

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

我不知道如何更改此代碼。我不明白RxJava!如果你有一些教程,請給我。我想了解RxJava的工作。

+0

你應該考慮學習改造和GSON第一,這樣你就不需要從一個HttpURLConnection類下載一個String手動解析JSON – EpicPandaForce

回答

0

您可以使用Observable和RETROFIT開始學習。這對於更好地瞭解RxJava是關鍵。

0

我創建了一個包含RxJava,Dagger,Retrofit和Gson的Repo。我認爲這會對你有很大的幫助。 Github link

如果有幫助,請考慮主演它。

0

這是怎樣的AsyncTask轉換爲RxAndroid:

Single.fromCallable(new Callable<LoginResponse>() { 
     @Override 
     public LoginResponse call() throws Exception { 
      // Doing something long in AysnTask doInBackGround off UI thread. 
      return yourResponse; 
     } 
    }) 
      .subscribeOn(Schedulers.io()) 
      .observeOn(AndroidSchedulers.mainThread()) 
      .doOnSuccess(new Consumer<LoginResponse>() { 
       @Override 
       public void accept(LoginResponse loginResponse) throws Exception { 
        // Onpost Execute with success in Main Thread 
       } 
      }) 
      .doOnError(new Consumer<Throwable>() { 
       @Override 
       public void accept(Throwable throwable) throws Exception { 
        // do on Error 
       } 
      }) 
      .subscribe();