0

I am trying to get weather data by passing the current latitude and longitude. How can I pass these two variable in the Retrofit Get Query string?

Please check below code, I want replace the {lat} and {lon} with the value I pass

@GET("yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22({lat},{lon})%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys")
Call<YahooWeather> getWeatherData(@Path("lat") String lat, @Path("lon") String lon);

Here is my Client interface YahooWeatherClient.java

public interface YahooWeatherClient {
    @GET("yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22({lat},{lon})%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys")
    Call<YahooWeather> getWeatherData(@Path("lat") String lat, @Path("lon") String lon);
}

Here is the Activity RetrofitActivity.java

public class RetrofitActivity extends AppCompatActivity {
    private static final String TAG = RetrofitActivity.class.getSimpleName();
    YahooWeatherClient mYahooWeatherClient;

    TextView messageTV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrofit);

        messageTV = (TextView) findViewById(R.id.message);

        initializeRetrofit();
        getWeatherData();
    }

    private void initializeRetrofit() {
        String BASE_URL = "https://query.yahooapis.com/v1/public/";

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        mYahooWeatherClient = retrofit.create(YahooWeatherClient.class);
    }

    private void getWeatherData() {
        Call<YahooWeather> weatherCall = mYahooWeatherClient.getWeatherData("23.780833", "90.349417");
        weatherCall.enqueue(new Callback<YahooWeather>() {
            @Override
            public void onResponse(Call<YahooWeather> call, Response<YahooWeather> response) {
                YahooWeather weather = response.body();
                messageTV.setText(weather.getQuery().getResults().getChannel().getAstronomy().getSunrise());
                Log.d(TAG, weather.getQuery().getResults().getChannel().getAstronomy().getSunrise());

            }

            @Override
            public void onFailure(Call<YahooWeather> call, Throwable t) {
                messageTV.setText(t.getMessage());
                Log.e(TAG, t.getMessage());
            }
        });
    }

}

Error Log:

                  --------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: net.deviac.retrofitsample, PID: 3576
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{net.deviac.retrofitsample/net.deviac.retrofitsample.RetrofitActivity}: java.lang.IllegalArgumentException: URL query string "q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22({lat},{lon})%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys" must not have replace block. For dynamic query parameters use @Query.
                      for method YahooWeatherClient.getWeatherData
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                      at android.app.ActivityThread.access$800(ActivityThread.java:151)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5254)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                   Caused by: java.lang.IllegalArgumentException: URL query string "q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22({lat},{lon})%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys" must not have replace block. For dynamic query parameters use @Query.
                      for method YahooWeatherClient.getWeatherData
                      at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:720)
                      at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:711)
                      at retrofit2.ServiceMethod$Builder.parseHttpMethodAndPath(ServiceMethod.java:297)
                      at retrofit2.ServiceMethod$Builder.parseMethodAnnotation(ServiceMethod.java:242)
                      at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:170)
                      at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:166)
                      at retrofit2.Retrofit$1.invoke(Retrofit.java:145)
                      at java.lang.reflect.Proxy.invoke(Proxy.java:397)
                      at $Proxy0.getWeatherData(Unknown Source)
                      at net.deviac.retrofitsample.RetrofitActivity.getWeatherData(RetrofitActivity.java:45)
                      at net.deviac.retrofitsample.RetrofitActivity.onCreate(RetrofitActivity.java:30)
                      at android.app.Activity.performCreate(Activity.java:5990)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                        ... 10 more
Simon Gomes
  • 383
  • 1
  • 5
  • 18
  • What problem are you facing? – Sangharsh Feb 06 '17 at 18:58
  • @Sangharsh please check the question... Actually I want to pass the lat and lon in the GET query string at the place of {lat} and {lon} – Simon Gomes Feb 06 '17 at 20:01
  • Your code seem to do that. Is it not putting lat, lon in query string? What is query string in outgoing request then? – Sangharsh Feb 06 '17 at 20:32
  • 2
    What is an example url? Do you perhaps want to use `@Query` instead of `@Path`? – Eric Cochran Feb 06 '17 at 23:06
  • @Sangharsh no it is not working in the query string it is passing the {lat} {lon} not the number i pass. Do you want me to update the question with Activity code and error log? – Simon Gomes Feb 07 '17 at 04:32
  • @EricCochran if it possible to achieve these using Query, could you please show me how? – Simon Gomes Feb 07 '17 at 04:33
  • @Simon https://github.com/square/retrofit/blob/9f2ea471e572f6596c482d23edcfeee0d861b959/samples/src/main/java/com/example/retrofit/JsonQueryParameters.java#L90 – Eric Cochran Feb 07 '17 at 04:46
  • @EricCochran this is not the same buddy. I want to replace something inside the query string instead of passing some additional query after the URL. – Simon Gomes Feb 07 '17 at 05:10
  • gotcha. that's why i asked for an example. You're going to have to do something clever in an OkHttp Interceptor or else build the full query at the call site. – Eric Cochran Feb 07 '17 at 05:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/135019/discussion-between-simon-and-eric-cochran). – Simon Gomes Feb 07 '17 at 05:42

2 Answers2

1

You can pass request parameters in Retrofit eg :

@GET("/yql")     //url which u want to pass the parameter 
void getSummary(@Query("lat") String lat,@Query("lon") String lon, Callback<YahooWeather> callback);

The above url will be passed like :

www.example.com/yql?lat=123.11&lon=132.12
Gokul
  • 931
  • 7
  • 16
0

I have done a similar thing in my project. I used retrofit to send the current latitude and longitude to the server and get response from the server.Here is the sample code.

Retrofit Interface

import java.util.List;
import java.util.StringTokenizer;
import in.invis.organickerala.modelclass.Medias;
import in.invis.organickerala.modelclass.NotificationDetail;
import in.invis.organickerala.modelclass.NotificationList;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;

/**
 * Created by VishnuM on 1/9/2017.
 */

public interface RegisterApi {

    @GET("orgkerala_poi.php")
    Call<List<Medias>>getResponse1(@Query("lat") Double lat, @Query("lon") Double lon);


    @GET("orgkerala_poi.php")
    Call<ResponseBody> getResponse2(@Query("lat") Double lat, @Query("lon") Double lon);

    @GET("orgpoi_detail.php")
    Call<List<NotificationDetail>> getResponse3(@Query("id") int id);

    @GET("orgpoi_detail.php")
    Call<ResponseBody> getResponse4(@Query("id") int id);

    @GET("orgpoi_list.php")
    Call<List<NotificationList>> getResponse5(@Query("id") int id);

    @GET("orgpoi_list.php")
    Call<ResponseBody> getResponse6(@Query("id") int id);


}

Java Code to get the response on passing the latitude and longitude

public  boolean download(  double lat,
                               double lon) {

         Log.d("Download","asbdah");
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(ROOT_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        RegisterApi apiservice =retrofit.create(RegisterApi.class);
        final Call<List<Medias>> detailresponse = apiservice.getResponse1(lat,lon);

        detailresponse.enqueue(new Callback<List<Medias>>() {
            @Override
            public void onResponse(Call<List<Medias>> call, Response<List<Medias>> response) {
                Log.d("asd00", "" + response);
                Log.d("asd00", "" + response.body().toString());
                Log.d("Resp112",""+ call.request().url().toString());

                // medias = response.body();
                // Log.d("mediasize",""+medias);
                media = (ArrayList<Medias>) response.body();

                Log.d("mediasize11",""+media.size());
            }

            @Override
            public void onFailure(Call<List<Medias>> call, Throwable t) {

            }
        });

If you are trying to do like this and need help ping me. Thankyou

Vishnu M Menon
  • 1,459
  • 2
  • 19
  • 34