- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我还是 Android 编程的新手。我正在学习一本名为 Learning Android 的书的教程。当我单击更新按钮时,我最终崩溃了。知道为什么吗?堆栈跟踪在下面。我遇到致命异常:main
package com.marakana.yamba;
import winterwell.jtwitter.Twitter;
import winterwell.jtwitter.TwitterException;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class StatusActivity extends Activity implements OnClickListener{
private static final String TAG ="StatusActivity";
Twitter twitter;
EditText editText;
Button updateButton ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.status);
// Find Views
editText = (EditText) findViewById(R.id.editText);
updateButton = (Button) findViewById(R.id.updateButton);
updateButton.setOnClickListener(this);
twitter = new Twitter("Yamba18", "undertaker");
twitter.setAPIRootUrl("http://yamba.marakana.com/api");
}
//Asynchronously Post to Twitter
class PostToTwitter extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... statuses){
try{
winterwell.jtwitter.Status status = twitter.updateStatus(statuses[0]);
return status.text;
}catch(TwitterException e){
Log.e(TAG,e.toString());
e.printStackTrace();
return "Failedd to Post";
}
}
@Override
protected void onProgressUpdate (Integer... values){
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result){
Toast.makeText(StatusActivity.this, result, Toast.LENGTH_LONG).show();
}
}
public void onClick(View v){
twitter.setStatus(editText.getText().toString());
Log.d(TAG, "onClicked");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.status, menu);
return true;
}
}
这是日志。
09-26 17:25:56.469: D/AndroidRuntime(612): Shutting down VM
09-26 17:25:56.469: W/dalvikvm(612): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
09-26 17:25:56.529: E/AndroidRuntime(612): FATAL EXCEPTION: main
09-26 17:25:56.529: E/AndroidRuntime(612): winterwell.jtwitter.TwitterException: java.io.IOException: Received authentication challenge is null
09-26 17:25:56.529: E/AndroidRuntime(612): at winterwell.jtwitter.URLConnectionHttpClient.processError(URLConnectionHttpClient.java:533)
09-26 17:25:56.529: E/AndroidRuntime(612): at winterwell.jtwitter.URLConnectionHttpClient.post2_connect(URLConnectionHttpClient.java:413)
09-26 17:25:56.529: E/AndroidRuntime(612): at winterwell.jtwitter.URLConnectionHttpClient.post2(URLConnectionHttpClient.java:379)
09-26 17:25:56.529: E/AndroidRuntime(612): at winterwell.jtwitter.URLConnectionHttpClient.post(URLConnectionHttpClient.java:348)
09-26 17:25:56.529: E/AndroidRuntime(612): at winterwell.jtwitter.Twitter.updateStatus(Twitter.java:2762)
09-26 17:25:56.529: E/AndroidRuntime(612): at winterwell.jtwitter.Twitter.updateStatus(Twitter.java:2694)
09-26 17:25:56.529: E/AndroidRuntime(612): at winterwell.jtwitter.Twitter.setStatus(Twitter.java:2482)
09-26 17:25:56.529: E/AndroidRuntime(612): at com.marakana.yamba.StatusActivity.onClick(StatusActivity.java:68)
09-26 17:25:56.529: E/AndroidRuntime(612): at android.view.View.performClick(View.java:3480)
09-26 17:25:56.529: E/AndroidRuntime(612): at android.view.View$PerformClick.run(View.java:13983)
09-26 17:25:56.529: E/AndroidRuntime(612): at android.os.Handler.handleCallback(Handler.java:605)
09-26 17:25:56.529: E/AndroidRuntime(612): at android.os.Handler.dispatchMessage(Handler.java:92)
09-26 17:25:56.529: E/AndroidRuntime(612): at android.os.Looper.loop(Looper.java:137)
09-26 17:25:56.529: E/AndroidRuntime(612): at android.app.ActivityThread.main(ActivityThread.java:4340)
09-26 17:25:56.529: E/AndroidRuntime(612): at java.lang.reflect.Method.invokeNative(Native Method)
09-26 17:25:56.529: E/AndroidRuntime(612): at java.lang.reflect.Method.invoke(Method.java:511)
09-26 17:25:56.529: E/AndroidRuntime(612): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-26 17:25:56.529: E/AndroidRuntime(612): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-26 17:25:56.529: E/AndroidRuntime(612): at dalvik.system.NativeStart.main(Native Method)
09-26 17:25:56.529: E/AndroidRuntime(612): Caused by: java.io.IOException: Received authentication challenge is null
09-26 17:25:56.529: E/AndroidRuntime(612): at libcore.net.http.HttpURLConnectionImpl.processAuthHeader(HttpURLConnectionImpl.java:397)
09-26 17:25:56.529: E/AndroidRuntime(612): at libcore.net.http.HttpURLConnectionImpl.processResponseHeaders(HttpURLConnectionImpl.java:345)
09-26 17:25:56.529: E/AndroidRuntime(612): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:276)
09-26 17:25:56.529: E/AndroidRuntime(612): at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:479)
09-26 17:25:56.529: E/AndroidRuntime(612): at winterwell.jtwitter.URLConnectionHttpClient.processError(URLConnectionHttpClient.java:468)
09-26 17:25:56.529: E/AndroidRuntime(612): ... 18 more
09-26 17:25:59.169: I/Process(612): Sending signal. PID: 612 SIG: 9
最佳答案
类似的例子对我有用,希望能有所帮助:
修改 MainActivity.java:
private static final String OAUTH_KEY = "your_OAUTH_KEY";
private static final String OAUTH_SECRET = "your_OAUTH_SECRET";
private static final String TWITTER_USER = "your_email";
我的文件:
MainActivity.java
package com.marakana.oauth;
import com.marakana.oauth.R;
import oauth.signpost.OAuth;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import oauth.signpost.exception.OAuthNotAuthorizedException;
import winterwell.jtwitter.OAuthSignpostClient;
import winterwell.jtwitter.Twitter;
import winterwell.jtwitter.TwitterException;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final String TAG = "OAuthDemo";
/********************************
Generate your values on the web::
https://dev.twitter.com/apps/
*********************************/
private static final String OAUTH_KEY = "your_OAUTH_KEY";
private static final String OAUTH_SECRET = "your_OAUTH_SECRET";
private static final String TWITTER_USER = "your_email";
private static final String OAUTH_CALLBACK_SCHEME = "x-marakana-oauth-twitter";
private static final String OAUTH_CALLBACK_URL = OAUTH_CALLBACK_SCHEME + "://callback";
private OAuthSignpostClient oauthClient;
private OAuthConsumer mConsumer;
private OAuthProvider mProvider;
private Twitter twitter;
SharedPreferences prefs;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mConsumer = new DefaultOAuthConsumer(OAUTH_KEY, OAUTH_SECRET);
//mConsumer = new CommonsHttpOAuthConsumer(OAUTH_KEY, OAUTH_SECRET);
mProvider = new DefaultOAuthProvider(
"https://api.twitter.com/oauth/request_token",
"https://api.twitter.com/oauth/access_token",
"https://api.twitter.com/oauth/authorize");
// Read the prefs to see if we have token
prefs = PreferenceManager.getDefaultSharedPreferences(this);
String token = prefs.getString("token", null);
String tokenSecret = prefs.getString("tokenSecret", null);
if (token != null && tokenSecret != null) {
// We have token, use it
mConsumer.setTokenWithSecret(token, tokenSecret);
// Make a Twitter object
oauthClient = new OAuthSignpostClient(OAUTH_KEY, OAUTH_SECRET,
token, tokenSecret);
twitter = new Twitter(TWITTER_USER, oauthClient);
}
}
/*
* Callback once we are done with the authorization of this app with
* Twitter.
*/
@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.d(TAG, "intent: " + intent);
// Check if this is a callback from OAuth
Uri uri = intent.getData();
if (uri != null && uri.getScheme().equals(OAUTH_CALLBACK_SCHEME)) {
Log.d(TAG, "callback: " + uri.getPath());
String verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
Log.d(TAG, "verifier: " + verifier);
new RetrieveAccessTokenTask().execute(verifier);
}
}
public void onClickAuthorize(View view) {
new OAuthAuthorizeTask().execute();
}
public void onClickTweet(View view) {
if (twitter == null) {
Toast.makeText(this, "Authenticate first", Toast.LENGTH_LONG)
.show();
return;
}
EditText status = (EditText) findViewById(R.id.status);
new PostStatusTask().execute(status.getText().toString());
}
public void onClickGetStatus(View view) {
if (twitter == null) {
Toast.makeText(this, "Authenticate first", Toast.LENGTH_LONG)
.show();
return;
}
new GetStatusTask().execute();
}
/* Responsible for starting the Twitter authorization */
class OAuthAuthorizeTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
String authUrl;
String message = null;
try {
authUrl = mProvider.retrieveRequestToken(mConsumer,
OAUTH_CALLBACK_URL);
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse(authUrl));
startActivity(intent);
} catch (OAuthMessageSignerException e) {
message = "OAuthMessageSignerException";
e.printStackTrace();
} catch (OAuthNotAuthorizedException e) {
message = "OAuthNotAuthorizedException";
e.printStackTrace();
} catch (OAuthExpectationFailedException e) {
message = "OAuthExpectationFailedException";
e.printStackTrace();
} catch (OAuthCommunicationException e) {
message = "OAuthCommunicationException";
e.printStackTrace();
}
return message;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (result != null) {
Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG)
.show();
}
}
}
/* Responsible for retrieving access tokens from twitter */
class RetrieveAccessTokenTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String message = null;
String verifier = params[0];
try {
// Get the token
Log.d(TAG, "mConsumer: " + mConsumer);
Log.d(TAG, "mProvider: " + mProvider);
mProvider.retrieveAccessToken(mConsumer, verifier);
String token = mConsumer.getToken();
String tokenSecret = mConsumer.getTokenSecret();
mConsumer.setTokenWithSecret(token, tokenSecret);
Log.d(TAG, String.format(
"verifier: %s, token: %s, tokenSecret: %s", verifier,
token, tokenSecret));
// Store token in prefs
prefs.edit().putString("token", token)
.putString("tokenSecret", tokenSecret).commit();
// Make a Twitter object
oauthClient = new OAuthSignpostClient(OAUTH_KEY, OAUTH_SECRET,
token, tokenSecret);
/* "NameTest" (any word) */
twitter = new Twitter("NameTest", oauthClient);
Log.d(TAG, "token: " + token);
} catch (OAuthMessageSignerException e) {
message = "OAuthMessageSignerException";
e.printStackTrace();
} catch (OAuthNotAuthorizedException e) {
message = "OAuthNotAuthorizedException";
e.printStackTrace();
} catch (OAuthExpectationFailedException e) {
message = "OAuthExpectationFailedException";
e.printStackTrace();
} catch (OAuthCommunicationException e) {
message = "OAuthCommunicationException";
e.printStackTrace();
}
return message;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (result != null) {
Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG)
.show();
}
}
}
/* Responsible for getting Twitter status */
class GetStatusTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
return twitter.getStatus().text;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
}
}
/* Responsible for posting new status to Twitter */
class PostStatusTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
try {
twitter.setStatus(params[0]);
return "Successfully posted: " + params[0];
} catch (TwitterException e) {
return "Error connecting to server.";
}
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.marakana.oauth"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="11" />
<uses-permission
android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:launchMode="singleInstance">
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- Used for OAuth callback -->
<intent-filter>
<action
android:name="android.intent.action.VIEW" />
<category
android:name="android.intent.category.DEFAULT" />
<category
android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="x-marakana-oauth-twitter"
android:host="callback" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textSize="50sp"
android:gravity="center" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Authenticate"
android:onClick="onClickAuthorize"
android:textSize="40sp"></Button>
<EditText
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/status"
android:textSize="40sp"
android:hint="What's up?"></EditText>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tweet"
android:onClick="onClickTweet"
android:textSize="40sp"></Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Status"
android:textSize="40sp"
android:onClick="onClickGetStatus"></Button>
</LinearLayout>
关于java - 异步任务致命异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19029435/
问题很简单:我正在寻找一种优雅的使用方式 CompletableFuture#exceptionally与 CompletableFuture#supplyAsync 一起.这是行不通的: priva
对于 Web 服务,我们通常使用 maven-jaxb2-plugin 生成 java bean,并在 Spring 中使用 JAXB2 编码。我想知道如何处理 WSDL/XSD 中声明的(SOAP-
这个问题已经有答案了: Array index out of bound behavior (10 个回答) 已关闭 8 年前。 我对下面的 C 代码感到好奇 int main(){
当在类的开头使用上下文和资源初始化 MediaPlayer 对象时,它会抛出 NullPointer 异常,但是当在类的开头声明它时(因此它是 null),然后以相同的方式初始化它在onCreate方
嘿 我尝试将 java 程序连接到 REST API。 使用相同的代码部分,我在 Java 6 中遇到了 Java 异常,并且在 Java 8 中运行良好。 环境相同: 信任 机器 unix 用户 代
我正在尝试使用 Flume 和 Hive 进行 Twitter 分析。为了从 twitter 获取推文,我在 flume.conf 文件中设置了所有必需的参数(consumerKey、consumer
我在 JavaFX 异常方面遇到一些问题。我的项目在我的 Eclipse 中运行,但现在我的 friend 也尝试访问该项目。我们已共享并直接保存到保管箱文件夹中。但他根本无法让它发挥作用。他在控制台
假设我使用 blur() 事件验证了电子邮件 ID,我正在这样做: $('#email').blur(function(){ //make ajax call , check if dupli
我这样做是为了从 C 代码调用非托管函数。 pCallback 是一个函数指针,因此在托管端是一个委托(delegate)。 [DllImport("MyDLL.dll")] public stati
为什么这段代码是正确的: try { } catch(ArrayOutOfBoundsException e) {} 这是错误的: try { } catch(IOException e) {} 这段
我遇到了以下问题:有导出函数的DLL。 代码示例如下:[动态链接库] __declspec(dllexport) int openDevice(int,void**) [应用] 开发者.h: __de
从其他线程,我知道我们不应该在析构函数中抛出异常!但是对于下面的例子,它确实有效。这是否意味着我们只能在一个实例的析构函数中抛出异常?我们应该如何理解这个代码示例! #include using n
为什么需要异常 引出 public static void main(String[
1. Java的异常机制 Throwable类是Java异常类型的顶层父类,一个对象只有是 Throwable 类的(直接或者间接)实例,他才是一个异常对象,才能被异常处理机制识别。JDK中内
我是 Python 的新手,我对某种异常方法的实现有疑问。这是代码(缩写): class OurException(Exception): """User defined Exception"
我已经创建了以下模式来表示用户和一组线程之间的关联,这些线程按他们的最后一条消息排序(用户已经阅读了哪些线程,哪些没有): CREATE TABLE table(user_id bigint, mes
我正在使用 Python 编写一个简单的自动化脚本,它可能会在多个位置引发异常。在他们每个人中,我都想记录一条特定的消息并退出程序。为此,我在捕获异常并处理它(执行特定的日志记录操作等)后引发 Sys
谁能解释一下为什么这会导致错误: let xs = [| "Mary"; "Mungo"; "Midge" |] Array.iter printfn xs 虽然不是这样: Array.iter pr
在我使用 Play! 的网站上,我有一个管理部分。所有 Admin Controller 都有一个 @With 和一个 @Check 注释。 断开连接后,一切正常。连接后,每次加载页面(任何页面,无论
我尝试连接到 azure 表存储并添加一个对象。它在本地主机上工作得很好,但是在我使用的服务器上我得到以下异常及其内部异常: Exception of type 'Microsoft.Wind
我是一名优秀的程序员,十分优秀!