gpt4 book ai didi

http post请求期间的Android :'Force Close'错误

转载 作者:可可西里 更新时间:2023-11-01 17:03:26 25 4
gpt4 key购买 nike

最初我的 Activity 使用 token 调用 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..which is:

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)

最佳答案

这是因为您正试图在 UI 线程上执行网络访问。您应该永远不要在此线程上执行可能代价高昂的操作(网络访问、文件 I/O、远程数据库连接、数据库事务等),因为它可能会阻塞 UI 线程并阻止您的布局充气/响应触摸事件。您需要将 HTTP 请求包装在 AsyncTask(推荐)或 Thread 中。

阅读我关于该主题的博文:

Why Ice Cream Sandwich Crashes Your App


此外,url 中有一个空格(笑)...这就是为什么您会在字符 122 上收到 IllegalArgumentException。从 中删除空格... 6880853& oauth_timestamp=2012 ...

我上面写的东西仍然适用。请,请,不要在 UI 线程上执行此任务。

关于http post请求期间的Android :'Force Close'错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11180036/

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