gpt4 book ai didi

java - 如何在 Android 项目中使用 SoundCloud API?

转载 作者:搜寻专家 更新时间:2023-11-01 08:55:15 25 4
gpt4 key购买 nike

我正在尝试使用 SoundCloud API 开发应用程序。我生成了 cient_id、client_secret、用户名和密码等。我还导入了所有与 API 相关的 java 文件,例如- ApiWrapper.javaCloudApi.javaCountingMultipartActivity.javaRequest.java 文件。但是,我在 Request.java 文件中遇到错误。我正在发布 link从哪里获取 API 文件。

当我执行应用程序时,我在 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");
}

}

最佳答案

onHomeClick() 方法在 UI 线程中运行并调用 SoundCloudManager.getInstance(),然后尝试登录包装器。这在 UI 线程中是不可能的,因为它必须访问 Internet。在后台线程中运行 SounCloud API 调用。

关于java - 如何在 Android 项目中使用 SoundCloud API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19488973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com