我正在尝试使用AsyncTask连接到Web服务。当我启动应用程序时,它意外关闭。我究竟做错了什么?这是我的代码:
public class Main extends Activity {
private static final String CONSUMER_KEY = "dsfjfsdksfdjl322342";
private static final String CONSUMER_SECRET = "fwefer234242424";
private TextView mText;
ProgressDialog progress = null;
TextView mTextView = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView = (TextView)findViewById(R.id.textView);
//new ConnectThinkNearTask().execute();
new ConnectToThinkSomeAPITask().execute("http://api-someapi.api.com/1.0/offers/40.7144,-74.0060.json");
}
private class ConnectToSomeAPITask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String...urls) {
String response = "";
for(String url: urls) {
OAuthConsumer consumer = new DefaultOAuthConsumer(CONSUMER_KEY,
CONSUMER_SECRET);
consumer.setTokenWithSecret("", "");
try {
URL url1 = new URL(url);
HttpURLConnection request = (HttpURLConnection) url1.openConnection();
// sign the request
consumer.sign(request);
// send the request
request.connect();
if(request.getResponseCode()==200) {
response = "Sorry, failed to connect to x";
// mText.setText("Sorry, failed to connect to x");
// } else if(request.getResponseCode()==401) {
response = "Congrats, you're connected to x!";
//mText.setText("Congrats, you're connected to x!");
} else
response = "Whatever you're asking for, it ain't a valid HTTP request...";
// mText.setText("Whatever you're asking for, it ain't a valid HTTP request...");
} catch (Exception e) {
e.printStackTrace();
}
} return response;
}
@Override
protected void onPostExecute(String result) {
mTextView.setText(result);
}
}
}
这是日志:
05-09 21:34:00.219: ERROR/AndroidRuntime(29878): FATAL EXCEPTION: AsyncTask #1
05-09 21:34:00.219: ERROR/AndroidRuntime(29878): java.lang.RuntimeException: An error occured while executing doInBackground()
05-09 21:34:00.219: ERROR/AndroidRuntime(29878): at android.os.AsyncTask$3.done(AsyncTask.java:200)
05-09 21:34:00.219: ERROR/AndroidRuntime(29878): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
05-09 21:34:00.219: ERROR/AndroidRuntime(29878): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
05-09 21:34:00.219: ERROR/AndroidRuntime(29878): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
05-09 21:34:00.219: ERROR/AndroidRuntime(29878): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-09 21:34:00.219: ERROR/AndroidRuntime(29878): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
05-09 21:34:00.219: ERROR/AndroidRuntime(29878): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
05-09 21:34:00.219: ERROR/AndroidRuntime(29878): at java.lang.Thread.run(Thread.java:1096)
05-09 21:34:00.219: ERROR/AndroidRuntime(29878): Caused by: java.lang.NoClassDefFoundError: oauth.signpost.basic.DefaultOAuthConsumer
05-09 21:34:00.219: ERROR/AndroidRuntime(29878): at org.androidstack.thinknear.Main$ConnectToThinkNearTask.doInBackground(Main.java:45)
05-09 21:34:00.219: ERROR/AndroidRuntime(29878): at org.androidstack.thinknear.Main$ConnectToThinkNearTask.doInBackground(Main.java:1)
05-09 21:34:00.219: ERROR/AndroidRuntime(29878): at android.os.AsyncTask$2.call(AsyncTask.java:185)
05-09 21:34:00.219: ERROR/AndroidRuntime(29878): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-09 21:34:00.219: ERROR/AndroidRuntime(29878): ... 4 more
答案 0 :(得分:0)
好吧,logcat很有帮助。当我通过选择用户库来配置构建路径时,只是将库添加到构建路径时,这种情况发生在我身上。出于某种原因,我需要在我的项目中始终创建一个名为libs的文件夹,然后添加jar并单击以添加到构建路径。它现在正在运作!