【问题标题】:Post image to Facebook in JSON format in Android在 Android 中以 JSON 格式将图像发布到 Facebook
【发布时间】:2011-01-25 14:08:20
【问题描述】:

我想将图像发布到我有我的 JSON 字符串的 Facebook,我在字符串中包含了静态图像,它工作正常,如果我想添加动态图像我该怎么办?

这是我的代码:

 parameters.putString(
    "attachment",
  "{\"name\":\""
  + b.getString("title")
  + "\",\"href\":\""
  + b.getString("cmpWeb")
  + "\",\"description\":\""
  + desc
 + "\",\"media\":[{\"type\":\"image\",\"src\":\"http://184.106.227.45/quaddeals/img/small_thumb/Deal/692.e6b86fa39f3ba25e29f0351140b57a94.jpg\",\"href\":\"http://alumni.brown.edu/\"}]}");

这个值是http://184.106.227.45/quaddeals/img/small_thumb/Deal/692.e6b86fa39f3ba25e29f0351140b57a94.jpg\" 静态值我想包含我使用intent从上一页得到的动态内容,比如(b.getString("url"))。

如果用户点击链接,我也想显示网页链接,它应该显示网页视图也是静态的“http://alumni.brown.edu”我想包含动态数据。

【问题讨论】:

    标签: android json


    【解决方案1】:

    嘿,我使用此代码使用 Json 上传图像。 如果您得到帮助,我会向您展示该代码。

    上传照片结果对话框.java

    public class UploadPhotoResultDialog extends Dialog {
    
        private String response, photo_id;
        private TextView mOutput, mUsefulTip;
        private Button mViewPhotoButton, mTagPhotoButton;
        private ImageView mUploadedPhoto;
        private Activity activity;
        private ProgressDialog dialog;
        private boolean hidePhoto = false;
        private Handler mHandler;
    
        public UploadPhotoResultDialog(Activity activity, String title, String response) {
            super(activity);
            this.activity = activity;
            this.response = response;
            setTitle(title);
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mHandler = new Handler();
    
            setContentView(R.layout.upload_photo_response);
            LayoutParams params = getWindow().getAttributes(); 
            params.width = LayoutParams.FILL_PARENT; 
            params.height = LayoutParams.FILL_PARENT; 
            getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
    
            mOutput = (TextView) findViewById(R.id.apiOutput);
            mUsefulTip = (TextView) findViewById(R.id.usefulTip);
            mViewPhotoButton = (Button) findViewById(R.id.view_photo_button);
            mTagPhotoButton = (Button) findViewById(R.id.tag_photo_button);
            mUploadedPhoto = (ImageView) findViewById(R.id.uploadedPhoto);
    
            JSONObject json;
            try {
                json = Util.parseJson(response);
                final String photo_id = json.getString("id");
                this.photo_id = photo_id;
    
                mOutput.setText(json.toString(2));
                mUsefulTip.setText(activity.getString(R.string.photo_tip));
                Linkify.addLinks(mUsefulTip, Linkify.WEB_URLS);
    
                mViewPhotoButton.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        if(hidePhoto) {
                            mViewPhotoButton.setText(R.string.view_photo);
                            hidePhoto = false;
                            mUploadedPhoto.setImageBitmap(null);
                        } else {
                            hidePhoto = true;
                            mViewPhotoButton.setText(R.string.hide_photo);
                            /*
                             * Source tag: view_photo_tag
                             */
                            Bundle params = new Bundle();
                            params.putString("fields", "picture");
                            dialog = ProgressDialog.show(activity, "", activity.getString(R.string.please_wait), true, true);
                            dialog.show();
                            Utility.mAsyncRunner.request(photo_id, params, new ViewPhotoRequestListener());
                        }
                    }
                });
                mTagPhotoButton.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        /*
                         * Source tag: tag_photo_tag
                         */
                        setTag();
                    }
                });
            } catch (JSONException e) {
                setText(activity.getString(R.string.exception) + e.getMessage());
            } catch (FacebookError e) {
                setText(activity.getString(R.string.facebook_error) + e.getMessage());
            }
        }
    
        public void setTag() {
            String relativePath = photo_id +"/tags/" + Utility.userUID;
            Bundle params = new Bundle();
            params.putString("x", "5");
            params.putString("y", "5");
            Utility.mAsyncRunner.request(relativePath, params, "POST", new TagPhotoRequestListener(), null);
        }
    
        public class ViewPhotoRequestListener extends BaseRequestListener {
    
            public void onComplete(final String response, final Object state) {
                try {
                    JSONObject json = Util.parseJson(response);
                    final String pictureURL = json.getString("picture");
                    if(TextUtils.isEmpty(pictureURL)) {
                        setText("Error getting \'picture\' field of the photo");
                    } else {
                        mHandler.post(new Runnable() {
                            public void run() {
                                new FetchImage().execute(pictureURL);
                            }
                        });
                    }
                 } catch (JSONException e) {
                     dialog.dismiss();
                     setText(activity.getString(R.string.exception) + e.getMessage());
                 } catch (FacebookError e) {
                     dialog.dismiss();
                     setText(activity.getString(R.string.facebook_error) + e.getMessage());
                 }
            }
    
            public void onFacebookError(FacebookError error) {
                dialog.dismiss();
                setText(activity.getString(R.string.facebook_error) + error.getMessage());
            }
        }
    
        public class TagPhotoRequestListener extends BaseRequestListener {
    
            public void onComplete(final String response, final Object state) {
                if (response.equals("true")) {
                    String message = "User tagged in photo at (5, 5)" + "\n";
                    message += "Api Response: " + response;
                    setText(message);
                } else {
                    setText("User could not be tagged.");
                }
            }
    
            public void onFacebookError(FacebookError error) {
                setText(activity.getString(R.string.facebook_error) + error.getMessage());
            }
        }
    
        public void setText(final String txt) {
            mHandler.post(new Runnable() {
                public void run() {
                    mOutput.setText(txt);
                }
            });
        }
    
        private class FetchImage extends AsyncTask<String, Void, Bitmap> {
             protected Bitmap doInBackground(String... urls) {
                 return Utility.getBitmap(urls[0]);
             }
    
             protected void onPostExecute(Bitmap result) {
                 dialog.dismiss();
                 mUploadedPhoto.setImageBitmap(result);
             }
        }
    }
    

    并且 Xml 字段:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:background="@color/black">
        <TextView 
            android:id="@+id/apiOutputLabel"
            android:text="@string/api_response"
            android:textColor="@color/white"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="3dp"
            android:paddingLeft="3dp" />
        <ScrollView   
            android:id="@+id/ScrollView01"  
            android:layout_height="wrap_content"   
            android:layout_width="fill_parent"
            android:paddingBottom="3dp">
            <TextView android:id="@+id/apiOutput"
                android:textColor="@color/white"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:paddingTop="3dp" 
                android:paddingLeft="3dp"
                android:background="@color/grey" />
        </ScrollView>
        <View
             android:paddingTop="3dp"
             android:layout_width="fill_parent"
             android:layout_height="2dip"
             android:background="@color/grey" />
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"> 
            <Button 
                android:id="@+id/view_photo_button"
                android:text="@string/view_photo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:padding="10dp" />
            <Button 
                android:id="@+id/tag_photo_button" 
                android:text="@string/tag_photo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:padding="10dp" />
        </LinearLayout>
        <ImageView 
                android:id="@+id/uploadedPhoto"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingTop="3dp"
                android:paddingBottom="3dp" />
        <TextView 
            android:id="@+id/tip_label"
            android:text="@string/tip_label"
            android:textColor="@color/white"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="3dp"
            android:paddingLeft="3dp" />
    
        <TextView 
            android:id="@+id/usefulTip"
            android:textColor="@color/white"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="5dp"
            android:paddingLeft="3dp" />
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-23
      • 2011-07-07
      • 2013-05-16
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      相关资源
      最近更新 更多