将照片上传到appcelerator云服务器

时间:2012-10-09 11:31:14

标签: android file-upload appcelerator

我正在尝试将照片上传到我的Android应用中的appcelerator云服务器。我已经按照appcelerator官方文档中给出的步骤进行了操作。现在没有错误。但Eclipse提供了照片上传所需的消息照片参数。 我被困在这里请帮助别人。以下是我的代码。

package com.example.test;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.appcelerator.cloud.sdk.CCMeta;
import com.appcelerator.cloud.sdk.CCRequestMethod;
import com.appcelerator.cloud.sdk.CCResponse;
import com.appcelerator.cloud.sdk.Cocoafish;
import com.appcelerator.cloud.sdk.CocoafishError;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;
import android.app.Activity;

public class MainActivity extends Activity {
String stringPath = "android.resource://com.example.test/raw/back.png";
Uri uri = Uri.parse(stringPath);
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Cocoafish sdk = new Cocoafish("ou5G8wrYyA0LKrVWmbZHRZgMpQ53sAH0");
    Map<String, Object> data = new HashMap<String, Object>();
    data.put("login", "test@appcelerator.com");
    data.put("password", "test_password");
    CCResponse response;
    try {
        response = sdk.sendRequest("users/login.json", CCRequestMethod.POST, data);
        JSONObject responseJSON = response.getResponseData();
        CCMeta meta = response.getMeta();
        if("ok".equals(meta.getStatus()) 
            && meta.getCode() == 200 
            && "loginUser".equals(meta.getMethod())) {

    //      Toast.makeText(getBaseContext(), "User Login Successfully",Toast.LENGTH_SHORT).show();
            Map<String, Object> data0 = new HashMap<String, Object>();
            data0.put("photo",R.drawable.back);
            CCResponse response0 = sdk.sendRequest("photos/create.json", CCRequestMethod.POST, data0);

            JSONObject responseJSON0 = response0.getResponseData();
            CCMeta meta0 = response0.getMeta();
            if("ok".equals(meta0.getStatus()) 
                && meta0.getCode() == 200 
                && "createPhoto".equals(meta0.getMethod())) {
              JSONArray photos = responseJSON0.getJSONArray("photos");

            }

        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (CocoafishError e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    }

}

1 个答案:

答案 0 :(得分:0)

替换

         data0.put("photo",R.drawable.back);

         data0.put("photo",new File("/sdcard/testImage.jpg"));