2012-12-14 52 views
1

我知道如何處理與Foursquare的API https://developer.foursquare.com/docs/photos/add如何使用foursquare從Android上傳圖片?

我嘗試這樣做:Sending images using Http Post

這裏是我的代碼:

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == CAMERA_PIC_REQUEST) { 
     if (resultCode == RESULT_OK) { 
      Uri imageUri = data.getData(); 
      List<NameValuePair> params = new ArrayList<NameValuePair>(4); 
      Intent intent = getIntent(); 
      String message = intent.getStringExtra(SignIn.EXTRA_MESSAGE); 
      params.add(new BasicNameValuePair("venueId", message)); 
      params.add(new BasicNameValuePair("image", imageUri.getPath())); 
      Log.d("myresponse", imageUri.getPath()); 
      params.add(new BasicNameValuePair("oauth_token", 
        [myoauthtoken])); 
      params.add(new BasicNameValuePair("v", "20121210")); 
      post("https://api.foursquare.com/v2/photos/add", params); 
     } 
    } 
} 

,但我得到這個堆棧跟蹤異常:

12-14 18:43:25.685: E/AndroidRuntime(15445): FATAL EXCEPTION: main 
12-14 18:43:25.685: E/AndroidRuntime(15445): java.lang.RuntimeException: Unable to resume activity {com.example.hobba/com.example.hobba.VenueCreated}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1337, result=-1, data=Intent { act=inline-data dat=content://media/external/images/media/10392 (has extras) }} to activity {com.example.hobba/com.example.hobba.VenueCreated}: android.os.NetworkOnMainThreadException 
12-14 18:43:25.685: E/AndroidRuntime(15445): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2616) 
12-14 18:43:25.685: E/AndroidRuntime(15445): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2644) 
12-14 18:43:25.685: E/AndroidRuntime(15445): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2130) 
12-14 18:43:25.685: E/AndroidRuntime(15445): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3553) 
12-14 18:43:25.685: E/AndroidRuntime(15445): at android.app.ActivityThread.access$700(ActivityThread.java:140) 
12-14 18:43:25.685: E/AndroidRuntime(15445): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1233) 
12-14 18:43:25.685: E/AndroidRuntime(15445): at android.os.Handler.dispatchMessage(Handler.java:99) 
12-14 18:43:25.685: E/AndroidRuntime(15445): at android.os.Looper.loop(Looper.java:137) 
12-14 18:43:25.685: E/AndroidRuntime(15445): at android.app.ActivityThread.main(ActivityThread.java:4898) 
12-14 18:43:25.685: E/AndroidRuntime(15445): at java.lang.reflect.Method.invokeNative(Native Method) 
12-14 18:43:25.685: E/AndroidRuntime(15445): at java.lang.reflect.Method.invoke(Method.java:511) 
12-14 18:43:25.685: E/AndroidRuntime(15445): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006) 
12-14 18:43:25.685: E/AndroidRuntime(15445): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773) 
12-14 18:43:25.685: E/AndroidRuntime(15445): at dalvik.system.NativeStart.main(Native Method) 
12-14 18:43:25.685: E/AndroidRuntime(15445): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1337, result=-1, data=Intent { act=inline-data dat=content://media/external/images/media/10392 (has extras) }} to activity {com.example.hobba/com.example.hobba.VenueCreated}: android.os.NetworkOnMainThreadException 
12-14 18:43:25.685: E/AndroidRuntime(15445): at android.app.ActivityThread.deliverResults(ActivityThread.java:3182) 
12-14 18:43:25.685: E/AndroidRuntime(15445): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2603) 
12-14 18:43:25.685: E/AndroidRuntime(15445): ... 13 more 
12-14 18:43:25.685: E/AndroidRuntime(15445): Caused by: android.os.NetworkOnMainThreadException 
12-14 18:43:25.685: E/AndroidRuntime(15445): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118) 
12-14 18:43:25.685: E/AndroidRuntime(15445): at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 
12-14 18:43:25.685: E/AndroidRuntime(15445): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 
12-14 18:43:25.685: E/AndroidRuntime(15445): at java.net.InetAddress.getAllByName(InetAddress.java:214) 
12-14 18:43:25.685: E/AndroidRuntime(15445): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137) 
12-14 18:43:25.685: E/AndroidRuntime(15445): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 
12-14 18:43:25.685: E/AndroidRuntime(15445): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 
12-14 18:43:25.685: E/AndroidRuntime(15445): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 
12-14 18:43:25.685: E/AndroidRuntime(15445): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670) 
12-14 18:43:25.685: E/AndroidRuntime(15445): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509) 
12-14 18:43:25.685: E/AndroidRuntime(15445): at com.example.hobba.VenueCreated.post(VenueCreated.java:152) 
12-14 18:43:25.685: E/AndroidRuntime(15445): at com.example.hobba.VenueCreated.onActivityResult(VenueCreated.java:66) 
12-14 18:43:25.685: E/AndroidRuntime(15445): at android.app.Activity.dispatchActivityResult(Activity.java:5368) 
12-14 18:43:25.685: E/AndroidRuntime(15445): at android.app.ActivityThread.deliverResults(ActivityThread.java:3178) 
12-14 18:43:25.685: E/AndroidRuntime(15445): ... 14 more 
+0

mime.content表示文本可能不爲空,因此請嘗試將圖像的MIME類型添加到Bundle中。 – DeeV

+0

@DeeV我該怎麼做? – Aboelseoud

+0

無論如何,錯誤已經改變。 – Aboelseoud

回答

0

您正試圖在您的UI線程上發出HTTP請求。 NetworkOnMainThreadException是個例外,這意味着您應該在後臺線程中完成您的工作。

爲了保證您的上傳,我建議您使用類似IntentService的內容來做您的HTTP發佈。除了documentation之外,您還可以查看我的example app,其中突出顯示了在UI線程之外完成工作的不同方法。