How to upload an image from Android using foursqua

2019-07-27 22:31发布

I know how to deal with foursquare API https://developer.foursquare.com/docs/photos/add

I tried this: Sending images using Http Post

Here's my code:

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);
        } 
    }
}

But I get an exception with this stack trace:

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

1条回答
来,给爷笑一个
2楼-- · 2019-07-27 23:13

You're attempting to make a HTTP request on your UI thread. The exception is NetworkOnMainThreadException, which means you should be doing your work in a background thread.

For doing your upload, I suggest using something like an IntentService to do your HTTP post. In addition to the documentation, you can also check out an example app of mine that highlights different methods for getting work done outside the UI thread.

查看更多
登录 后发表回答