如何上传使用四方于Android的形象呢

如何上传使用四方于Android的形象呢

本文介绍了如何上传使用四方于Android的形象呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何处理与Foursquare的API

我试过这样:

下面是我的code:

 保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
    如果(要求code == CAMERA_PIC_REQUEST){
        如果(结果code == RESULT_OK){
            乌里imageUri = data.getData();
            清单<&的NameValuePair GT; PARAMS =新的ArrayList<&的NameValuePair GT;(4);
            意向意图= getIntent();
            字符串消息= intent.getStringExtra(SignIn.EXTRA_MESSAGE);
            params.add(新BasicNameValuePair(venueId,消息));
            params.add(新BasicNameValuePair(形象,imageUri.getPath()));
            Log.d(myresponse,imageUri.getPath());
            params.add(新BasicNameValuePair(组oauth_token
                    [myoauthtoken]));
            params.add(新BasicNameValuePair(V,20121210));
            后(https://api.foursquare.com/v2/photos/add,则params);
        }
    }
}

但是我得到这个堆栈跟踪的异常:

  18 12-14:43:25.685:E / AndroidRuntime(15445):致命异常:主要
12-14 18:43:25.685:E / AndroidRuntime(15445):了java.lang.RuntimeException:无法恢复活动{com.example.hobba / com.example.hobba.VenueCreated}了java.lang.RuntimeException:传递失败结果ResultInfo {谁= NULL,请求= 1337,结果= -1,数据= {意向A​​CT =直列数据DAT =内容://媒体/外部/图像/媒体/ 10392(有临时演员)}}到活动{COM .example.hobba / com.example.hobba.VenueCreated}:android.os.NetworkOnMainThreadException
12-14 18:43:25.685:E / AndroidRuntime(15445):在android.app.ActivityThread.performResumeActivity(ActivityThread.java:2616)
12-14 18:43:25.685:E / AndroidRuntime(15445):在android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2644)
12-14 18:43:25.685:E / AndroidRuntime(15445):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2130)
12-14 18:43:25.685:E / AndroidRuntime(15445):在android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3553)
12-14 18:43:25.685:E / AndroidRuntime(15445):在android.app.ActivityThread.access $ 700(ActivityThread.java:140)
12-14 18:43:25.685:E / AndroidRuntime(15445):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1233)
12-14 18:43:25.685:E / AndroidRuntime(15445):在android.os.Handler.dispatchMessage(Handler.java:99)
12-14 18:43:25.685:E / AndroidRuntime(15445):在android.os.Looper.loop(Looper.java:137)
12-14 18:43:25.685:E / AndroidRuntime(15445):在android.app.ActivityThread.main(ActivityThread.java:4898)
12-14 18:43:25.685:E / AndroidRuntime(15445):在java.lang.reflect.Method.invokeNative(本机方法)
12-14 18:43:25.685:E / AndroidRuntime(15445):在java.lang.reflect.Method.invoke(Method.java:511)
12-14 18:43:25.685:E / AndroidRuntime(15445):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1006)
12-14 18:43:25.685:E / AndroidRuntime(15445):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
12-14 18:43:25.685:E / AndroidRuntime(15445):在dalvik.system.NativeStart.main(本机方法)
12-14 18:43:25.685:E / AndroidRuntime(15445):了java.lang.RuntimeException:由故障造成的结果交付{ResultInfo谁= NULL,请求= 1337,结果= -1,数据= {意向A​​CT =直列-data DAT =内容://媒体/外部/图像/媒体/ 10392(有临时演员)}}到活动{com.example.hobba / com.example.hobba.VenueCreated}:android.os.NetworkOnMainThreadException
12-14 18:43:25.685:E / AndroidRuntime(15445):在android.app.ActivityThread.deliverResults(ActivityThread.java:3182)
12-14 18:43:25.685:E / AndroidRuntime(15445):在android.app.ActivityThread.performResumeActivity(ActivityThread.java:2603)
12-14 18:43:25.685:E / AndroidRuntime(15445):... 13个
12-14 18:43:25.685:E / AndroidRuntime(15445):由:android.os.NetworkOnMainThreadException
12-14 18:43:25.685:E / AndroidRuntime(15445):在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
12-14 18:43:25.685:E / AndroidRuntime(15445):在java.net.InetAddress.lookupHostByName(InetAddress.java:385)
12-14 18:43:25.685:E / AndroidRuntime(15445):在java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
12-14 18:43:25.685:E / AndroidRuntime(15445):在java.net.InetAddress.getAllByName(InetAddress.java:214)
12-14 18:43:25.685:E / AndroidRuntime(15445):在org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
12-14 18:43:25.685:E / AndroidRuntime(15445):在org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
12-14 18:43:25.685:E / AndroidRuntime(15445):在org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
12-14 18:43:25.685:E / AndroidRuntime(15445):在org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
12-14 18:43:25.685:E / AndroidRuntime(15445):在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
12-14 18:43:25.685:E / AndroidRuntime(15445):在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
12-14 18:43:25.685:E / AndroidRuntime(15445):在com.example.hobba.VenueCreated.post(VenueCreated.java:152)
12-14 18:43:25.685:E / AndroidRuntime(15445):在com.example.hobba.VenueCreated.onActivityResult(VenueCreated.java:66)
12-14 18:43:25.685:E / AndroidRuntime(15445):在android.app.Activity.dispatchActivityResult(Activity.java:5368)
12-14 18:43:25.685:E / AndroidRuntime(15445):在android.app.ActivityThread.deliverResults(ActivityThread.java:3178)
12-14 18:43:25.685:E / AndroidRuntime(15445):... 14个


解决方案

您正试图使你的UI线程HTTP请求。唯一的例外是 NetworkOnMainThreadException ,这意味着你应该做你的工作在后台线程。

有关做你的上传,我建议使用类似的做你的HTTP发布。除了,您还可以检查出一个例如应用,突出获取UI线程之外完成的工作不同的方法。

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
解决方案

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.

这篇关于如何上传使用四方于Android的形象呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 23:55