3

I am creating an app with SoundCloud api and trying to append the string value which is entered by user but it showing me null in interface how can I append that in url?

MainActivity.java

  b.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View p1)
            {
                // TODO: Implement this method
                search = et.getText().toString();
                Log.d("search",search);
                Intent i = new Intent(MainActivity.this, SoundCloud.class);
                i.putExtra("mysearch", search);
                startActivity(i);

                Log.d("CustomUrl",SCService.CustomUrl);
                Toast.makeText(MainActivity.this,""+SCService.CustomUrl,Toast.LENGTH_SHORT).show();


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

                SCService Scservice = retrofit.create(SCService.class);
                Call<Track> call = Scservice.getTrack(search);
                call.enqueue(new Callback<Track>(){

                        @Override
                        public void onResponse(Call<Track> call, Response<Track> response)
                        {
                            // TODO: Implement this method
                            if (response.isSuccessful())
                            {
                                Track track = response.body();
                                streamUrl = track.getStreamUrl();
                                trackname = track.getTitle();
                                Log.e("track", track.getTitle());
                                Toast.makeText(MainActivity.this, trackname, Toast.LENGTH_SHORT).show();
                                Toast.makeText(MainActivity.this, "url" + streamUrl, Toast.LENGTH_SHORT).show();
                                //addToList(trackname);
                            }
                        }

                        @Override
                        public void onFailure(Call<Track> p1, Throwable p2)
                        {
                            // TODO: Implement this method
                            Toast.makeText(MainActivity.this,"error"+p2.getMessage(),Toast.LENGTH_SHORT).show();
                            //showMessage("Network Error: " +  p2.getMessage());
                            Log.d("error", p2.getMessage());
                        }
                    });

            }
        });

ScService.java

public interface SCService
{
    public String CustomUrl = Config.API_URL+"/resolve.json?url="+SoundCloud.SCURL+"&client_id="+Config.CLIENT_ID;
@GET("/resolve.json?url={SCURL}"+"&client_id="+Config.CLIENT_ID)
Call<Track> getTrack(@Path("SCURL")String SCURL);
}       

Searched string is in my SoundCloud.java class

    SCURL = receivedmsg.getString("mysearch");
    Log.d("intentdata",SCURL);

    tv.setText("Search Results for " + SCURL);  

I want append that SCURL String in GET parameter but it showing me null value for SCURL, how could i achieve this?

Update: I have changed my code as above but got this error

enter code here
  03-15 13:22:14.866 6149 6149 D     search com.sk.scdoenloader                   /https://m.soundcloud.com/dharmaworldwide/houseofcards-mixmaster-05b
  03-15 13:22:14.967 6149 6149 D     CustomUrl com.sk.scdoenloader                https://api.soundcloud.com/resolve.json?url=null&client_id=iZIs9mchVcX5lhVRyQGGAYlNPVldzAoX
  03-15 13:22:15.299 6149 6149 E     AndroidRuntime com.sk.scdoenloader                   FATAL EXCEPTION: main
  03-15 13:22:15.299 6149 6149 E     AndroidRuntime com.sk.scdoenloader           Process: com.sk.scdoenloader, PID: 6149
  03-15 13:22:15.299 6149 6149 E     AndroidRuntime com.sk.scdoenloader           java.lang.IllegalArgumentException: No Retrofit annotation found. (parameter #1)
  03-15 13:22:15.299 6149 6149 E     AndroidRuntime com.sk.scdoenloader           for method SCService.getTrack
  03-15 13:22:15.299 6149 6149 E     AndroidRuntime com.sk.scdoenloader           at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:720)
  03-15 13:22:15.299 6149 6149 E     AndroidRuntime com.sk.scdoenloader           at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:711)
  03-15 13:22:15.299 6149 6149 E     AndroidRuntime com.sk.scdoenloader           at retrofit2.ServiceMethod$Builder.parameterError(ServiceMethod.java:729)
  03-15 13:22:15.299 6149 6149 E     AndroidRuntime com.sk.scdoenloader                     
ericn
  • 12,476
  • 16
  • 84
  • 127
shubham kumbhar
  • 47
  • 1
  • 2
  • 10

4 Answers4

12

You have to use @Path here

@GET("/resolve.json?url={url}&client_id={clientId}")
Call<Track> getTrack(@Path("url") String url,@Path("clientId") String clientId);
Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
Ramkumar.M
  • 681
  • 6
  • 21
6

You can use DYNAMIC URL with retrofit !! (With @Get )

public interface SCService{  
   @GET
   public Call<Track> getTrack(@Url String url);
}

Then you can call with your Custom string url

 service.getTrack("/resolve.json?url="+SoundCloud.SCURL+"&client_id="+ Config.CLIENT_ID + SCURL)
Santanu Sur
  • 10,997
  • 7
  • 33
  • 52
1

What you want here are GET parameters, not path, so use @Query("scurl") String scurl

More details are here

By the way, it's kinda strange (at least in Java) to use all caps for your variable name, scurl is more appropriate than SCURL here

ericn
  • 12,476
  • 16
  • 84
  • 127
  • I used Asynctask now and my project is completed but thanks for your answer i will try this next time, I made that SCURL as static variable so made it uppercase :) – shubham kumbhar Mar 18 '18 at 12:26
  • I understand that SCURL is a static final of class SoundCloud. However, within the context of `Call getTrack(@Path("SCURL")String SCURL);` method, it's a normal variable and all caps should not be used ;) – ericn Mar 19 '18 at 05:29
1

You have to use @Query here

 @GET("Account/RegisterMobileNumber")
 Call<JsonObject> sendSMSToMobileNumber(@Query("mobileNumber") String mobileNumber);
Iman Marashi
  • 5,593
  • 38
  • 51