1

我知道如何处理foursquare API https://developer.foursquare.com/docs/photos/add

我试过这个:使用 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
4

1 回答 1

0

您正在尝试在 UI 线程上发出 HTTP 请求。例外是NetworkOnMainThreadException,这意味着您应该在后台线程中进行工作。

为了进行上传,我建议使用IntentService 之类的东西来进行 HTTP 发布。除了文档之外,您还可以查看我的一个示例应用程序,它突出显示了在 UI 线程之外完成工作的不同方法。

于 2012-12-14T16:51:25.897 回答