最初我的活动new DropboxApi(token).execute();
使用令牌调用。
public class DropboxApi extends AsyncTask<Void, Long, Boolean> {
public String token;
public DropboxApi(String a) {
// TODO Auto-generated constructor stub
token=a;
}
String HMAC_SHA1_ALGORITHM = "HmacSHA1";
public String Sign(String token)// throws Exception
{
String uri = "https://api.dropbox.com/1/account/info";
//Uri json = GetResponse(uri);
//return ParseJson<Account>(json);
uri+="?";
String queryParams = "oauth_consumer_key=2f2y1dyuqhp58ek&oauth_token="+token+"&oauth_nonce=6880853&oauth_timestamp=";
java.util.Date date= new java.util.Date();
//System.out.println(new Timestamp(date.getTime()));
queryParams+=(new Timestamp(date.getTime()).toString())+"&oauth_signature_method=HMAC-SHA1&oauth_version=1.0&oauth_signature=";
uri+=queryParams;
Mac mac=null;
try {
mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Key key=new SecretKeySpec("2f2y1dyuqhp58ek".getBytes(), HMAC_SHA1_ALGORITHM) ;
try {
mac.init(key);
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] rawHmac = mac.doFinal(queryParams.getBytes());
String result=new String(Base64.encode(rawHmac,Base64.DEFAULT));
uri+=result;
return uri;
}
@Override
protected Boolean doInBackground(Void... params) {
// TODO Auto-generated method stub
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(Sign(token));
int k=0;
k=23;
HttpResponse response;
try {
int j=3;
j=0;
response = httpclient.execute(httpPost); // the request executes
Log.d("HTTP","Executed");
String responseBody = EntityUtils.toString(response.getEntity());
} catch (ClientProtocolException e) {
e.printStackTrace();
}
catch(ConnectTimeoutException e){
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
//return null;
return null;
}
}
在调试中,代码工作正常,直到它返回 uri.. 这是:
https://api.dropbox.com/1/account/info?oauth_consumer_key=2f2y1dyuqhp58ek&oauth_token=wz27t6thob0fbxl&oauth_nonce=6880853& oauth_timestamp=2012-06-24 23:46:51.734&oauth_signature_method=HMAC-SHA1&oauth_version=1.0&oauth_signature=s8zaFHmQJ24BTxTYV4VUSLdV9d8=
这是我第一次发出http请求..这段代码应该工作吗?
06-24 23:44:47.364: E/AndroidRuntime(11315): at dalvik.system.NativeStart.main(Native Method)
06-24 23:44:53.033: I/Process(11315): Sending signal. PID: 11315 SIG: 9
06-24 23:45:43.404: W/ActivityThread(11375): Application cloud.mobile is waiting for the debugger on port 8100...
06-24 23:45:43.563: I/System.out(11375): Sending WAIT chunk
06-24 23:45:43.683: I/dalvikvm(11375): Debugger is active
06-24 23:45:43.846: I/System.out(11375): Debugger has connected
06-24 23:45:43.887: I/System.out(11375): waiting for debugger to settle...
06-24 23:45:44.087: I/System.out(11375): waiting for debugger to settle...
06-24 23:45:44.338: I/System.out(11375): waiting for debugger to settle...
06-24 23:45:44.544: I/System.out(11375): waiting for debugger to settle...
06-24 23:45:44.773: I/System.out(11375): waiting for debugger to settle...
06-24 23:45:44.983: I/System.out(11375): waiting for debugger to settle...
06-24 23:45:45.183: I/System.out(11375): waiting for debugger to settle...
06-24 23:45:45.393: I/System.out(11375): waiting for debugger to settle...
06-24 23:45:45.594: I/System.out(11375): waiting for debugger to settle...
06-24 23:45:45.805: I/System.out(11375): waiting for debugger to settle...
06-24 23:45:46.004: I/System.out(11375): waiting for debugger to settle...
06-24 23:45:46.214: I/System.out(11375): waiting for debugger to settle...
06-24 23:45:46.414: I/System.out(11375): waiting for debugger to settle...
06-24 23:45:46.627: I/System.out(11375): waiting for debugger to settle...
06-24 23:45:46.833: I/System.out(11375): waiting for debugger to settle...
06-24 23:45:47.034: I/System.out(11375): waiting for debugger to settle...
06-24 23:45:47.254: I/System.out(11375): waiting for debugger to settle...
06-24 23:45:47.466: I/System.out(11375): waiting for debugger to settle...
06-24 23:45:47.663: I/System.out(11375): waiting for debugger to settle...
06-24 23:45:47.873: I/System.out(11375): waiting for debugger to settle...
06-24 23:45:48.073: I/System.out(11375): waiting for debugger to settle...
06-24 23:45:48.283: I/System.out(11375): waiting for debugger to settle...
06-24 23:45:48.483: I/System.out(11375): waiting for debugger to settle...
06-24 23:45:48.684: I/System.out(11375): waiting for debugger to settle...
06-24 23:45:48.894: I/System.out(11375): waiting for debugger to settle...
06-24 23:45:49.094: I/System.out(11375): waiting for debugger to settle...
06-24 23:45:49.295: I/System.out(11375): waiting for debugger to settle...
06-24 23:45:49.504: I/System.out(11375): waiting for debugger to settle...
06-24 23:45:49.714: I/System.out(11375): waiting for debugger to settle...
06-24 23:45:49.915: I/System.out(11375): waiting for debugger to settle...
06-24 23:45:50.123: I/System.out(11375): waiting for debugger to settle...
06-24 23:45:50.323: I/System.out(11375): waiting for debugger to settle...
06-24 23:45:50.524: I/System.out(11375): waiting for debugger to settle...
06-24 23:45:50.754: I/System.out(11375): debugger has settled (1426)
06-24 23:45:53.493: D/dalvikvm(11375): GC_EXTERNAL_ALLOC freed 721 objects / 53976 bytes in 321ms
06-24 23:48:29.164: D/AndroidRuntime(11375): Shutting down VM
06-24 23:48:29.164: W/dalvikvm(11375): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-24 23:48:29.423: E/AndroidRuntime(11375): FATAL EXCEPTION: main
06-24 23:48:29.423: E/AndroidRuntime(11375): java.lang.IllegalArgumentException: Illegal character in query at index 122: https://api.dropbox.com/1/account/info?oauth_consumer_key=2f2y1dyuqhp58ek&oauth_token=wz27t6thob0fbxl&oauth_nonce=6880853& oauth_timestamp=2012-06-24 23:46:51.734&oauth_signature_method=HMAC-SHA1&oauth_version=1.0&oauth_signature=s8zaFHmQJ24BTxTYV4VUSLdV9d8=
06-24 23:48:29.423: E/AndroidRuntime(11375): at java.net.URI.create(URI.java:970)
06-24 23:48:29.423: E/AndroidRuntime(11375): at org.apache.http.client.methods.HttpPost.<init>(HttpPost.java:79)
06-24 23:48:29.423: E/AndroidRuntime(11375): at cloud.mobile.DropboxApi.httpMethod(DropboxApi.java:67)
06-24 23:48:29.423: E/AndroidRuntime(11375): at cloud.mobile.MCActivity$3.onClick(MCActivity.java:161)
06-24 23:48:29.423: E/AndroidRuntime(11375): at android.view.View.performClick(View.java:2408)
06-24 23:48:29.423: E/AndroidRuntime(11375): at android.view.View$PerformClick.run(View.java:8816)
06-24 23:48:29.423: E/AndroidRuntime(11375): at android.os.Handler.handleCallback(Handler.java:587)
06-24 23:48:29.423: E/AndroidRuntime(11375): at android.os.Handler.dispatchMessage(Handler.java:92)
06-24 23:48:29.423: E/AndroidRuntime(11375): at android.os.Looper.loop(Looper.java:123)
06-24 23:48:29.423: E/AndroidRuntime(11375): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-24 23:48:29.423: E/AndroidRuntime(11375): at java.lang.reflect.Method.invokeNative(Native Method)
06-24 23:48:29.423: E/AndroidRuntime(11375): at java.lang.reflect.Method.invoke(Method.java:521)
06-24 23:48:29.423: E/AndroidRuntime(11375): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-24 23:48:29.423: E/AndroidRuntime(11375): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-24 23:48:29.423: E/AndroidRuntime(11375): at dalvik.system.NativeStart.main(Native Method)