Amit Jayaswal
Amit Jayaswal

Reputation: 1723

How to use SoundCloud API in Android Project.?

I am trying to develop an app using the SoundCloud API. I've generated the cient_id, client_secret, username and password etc. I've also imported all API related java files, like- ApiWrapper.java,CloudApi.java, CountingMultipartActivity.java and Request.java file.However ,I am getting error in Request.java File. I am posting the link from where i get the API files.

When I am executing the app then i am getting following errors in LogCat.:-

10-21 12:41:15.096: E/AndroidRuntime(13524): FATAL EXCEPTION: main
10-21 12:41:15.096: E/AndroidRuntime(13524): java.lang.IllegalStateException: Could not execute method of the activity
10-21 12:41:15.096: E/AndroidRuntime(13524):  at android.view.View$1.onClick(View.java:3071)
10-21 12:41:15.096: E/AndroidRuntime(13524):  at android.view.View.performClick(View.java:3538)
10-21 12:41:15.096: E/AndroidRuntime(13524):  at android.view.View$PerformClick.run(View.java:14330)
10-21 12:41:15.096: E/AndroidRuntime(13524):  at android.os.Handler.handleCallback(Handler.java:608)
10-21 12:41:15.096: E/AndroidRuntime(13524):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-21 12:41:15.096: E/AndroidRuntime(13524):  at android.os.Looper.loop(Looper.java:156)
10-21 12:41:15.096: E/AndroidRuntime(13524):  at android.app.ActivityThread.main(ActivityThread.java:4987)
10-21 12:41:15.096: E/AndroidRuntime(13524):  at java.lang.reflect.Method.invokeNative(Native Method)
10-21 12:41:15.096: E/AndroidRuntime(13524):  at java.lang.reflect.Method.invoke(Method.java:511)
10-21 12:41:15.096: E/AndroidRuntime(13524):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-21 12:41:15.096: E/AndroidRuntime(13524):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-21 12:41:15.096: E/AndroidRuntime(13524):  at dalvik.system.NativeStart.main(Native Method)
10-21 12:41:15.096: E/AndroidRuntime(13524): Caused by: java.lang.reflect.InvocationTargetException
10-21 12:41:15.096: E/AndroidRuntime(13524):  at java.lang.reflect.Method.invokeNative(Native Method)
10-21 12:41:15.096: E/AndroidRuntime(13524):  at java.lang.reflect.Method.invoke(Method.java:511)
10-21 12:41:15.096: E/AndroidRuntime(13524):  at android.view.View$1.onClick(View.java:3066)
10-21 12:41:15.096: E/AndroidRuntime(13524):  ... 11 more
10-21 12:41:15.096: E/AndroidRuntime(13524): Caused by: android.os.NetworkOnMainThreadException
10-21 12:41:15.096: E/AndroidRuntime(13524):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1186)
10-21 12:41:15.096: E/AndroidRuntime(13524):  at java.net.InetAddress.lookupHostByName(InetAddress.java:394)
10-21 12:41:15.096: E/AndroidRuntime(13524):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:245)
10-21 12:41:15.096: E/AndroidRuntime(13524):  at java.net.InetAddress.getAllByName(InetAddress.java:220)
10-21 12:41:15.096: E/AndroidRuntime(13524):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
10-21 12:41:15.096: E/AndroidRuntime(13524):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10-21 12:41:15.096: E/AndroidRuntime(13524):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
10-21 12:41:15.096: E/AndroidRuntime(13524):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
10-21 12:41:15.096: E/AndroidRuntime(13524):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:591)
10-21 12:41:15.096: E/AndroidRuntime(13524):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:532)
10-21 12:41:15.096: E/AndroidRuntime(13524):  at com.soundcloud.api.ApiWrapper.safeExecute(ApiWrapper.java:565)
10-21 12:41:15.096: E/AndroidRuntime(13524):  at com.soundcloud.api.ApiWrapper.requestToken(ApiWrapper.java:263)
10-21 12:41:15.096: E/AndroidRuntime(13524):  at com.soundcloud.api.ApiWrapper.login(ApiWrapper.java:149)
10-21 12:41:15.096: E/AndroidRuntime(13524):  at com.example.footballchants.SoundCloudManager.init(SoundCloudManager.java:41)
10-21 12:41:15.096: E/AndroidRuntime(13524):  at com.example.footballchants.SoundCloudManager.<init>(SoundCloudManager.java:32)
10-21 12:41:15.096: E/AndroidRuntime(13524):  at com.example.footballchants.SoundCloudManager.getInstance(SoundCloudManager.java:27)
10-21 12:41:15.096: E/AndroidRuntime(13524):  at com.example.footballchants.FindChantsActivity.onHomeClick(FindChantsActivity.java:127)

API:

public class SoundCloudManager {
    //starting of Singleton
    private static SoundCloudManager mInstance = null;
     ApiWrapper wrapper;

    public static SoundCloudManager getInstance()
    {
        if(mInstance== null)
        {
            mInstance= new SoundCloudManager();
        }
        return mInstance;
    }
    private SoundCloudManager(){
        init();
    }
    //ending of Singleton.

    public void init()
    {
        ApiWrapper wrapper = new ApiWrapper("XXX", "XXX", null, null);

        try {
            wrapper.login("XXXX", "XXXX");
            HttpResponse resp = wrapper.get(Request.to("/me"));

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //return wrapper;

    }

}


Now i develop in RingtonesActivity. In the RingtonesActivity i used Singletun class, in OnHomeClick(). But i am not getting any error, stil my app is getting crash.

public class RingtonesActivity extends Activity {


    private ListView listView;
    private ArrayList<Integer> drawableData = new ArrayList<Integer>();
    private RingtonesMusicAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ringtones);
        listView = (ListView) findViewById(R.id.listView);

        for (int i = 0; i < 5; i++) {
            drawableData.add(R.drawable.phone_small_icon);
        }

        adapter = new RingtonesMusicAdapter(this, R.layout.activity_ringtones_row,
                drawableData);

        listView.setAdapter(adapter);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.ringtones, menu);
        return true;
    }

    public void onHomeClick(View view){
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        SoundCloudManager sound = SoundCloudManager.getInstance();
      //  sound.init();
        System.out.println("Yuuuhuuu");
    }

}

Upvotes: 2

Views: 2030

Answers (1)

and10
and10

Reputation: 46

The onHomeClick() method runs in the UI thread and calls SoundCloudManager.getInstance(), which then tries to Login the wrapper. That is not possible from the UI thread because it must access the Internet. Run the SounCloud API calls in a Background thread.

Upvotes: 3

Related Questions