使用文件正文在服务器上的上载映像中找不到文件异常

时间:2014-07-16 13:51:41

标签: android exception http-post

我的要求是登录Facebook,登录后,Facebook信息将是我的应用程序个人资料信息,但当我尝试用facebook个人资料图片网址保存我的个人资料时,使用FileBody覆盖页面图片网址,它给我文件未找到异常,但个人资料图片网址和封面网址在浏览器中正常工作并显示图片。

任何人都可以帮我解决这个问题。

public String[] saveProfile(Context context, String bannerPath, String profilePicPath, String userName)
{

String str_response = "";
String[] arryResponse = new String[2];

mContext = context;

Log.d(TAG, " --  profilePicPath -- " + profilePicPath);
Log.d(TAG, " --  bannerPath -- " + bannerPath);

if (isNetworkConnected())
{
    HttpClient httpclient = new DefaultHttpClient();
    HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), 10000);
    HttpPost httppost = new HttpPost(baseUrl + "user/edituserprofile/");
    try
    {
        MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

    if (profilePicPath.length() > 0)
    {
        profilePicPath = profilePicPath.replace("http", "https").toString().trim();
        File F = new File(profilePicPath);
        FileBody fb = new FileBody(F);
        reqEntity.addPart("vProfilePicture", fb);
    }

    if (bannerPath.length() > 0)
    {
        bannerPath = bannerPath.replace("http", "https").toString().trim();
        File F = new File(bannerPath.replace("http", "https"));
        FileBody fb = new FileBody(F);
        reqEntity.addPart("vCoverPicture", fb);
    }


    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    String x_api_key = (sharedPreferences.getString(ConstantCode.X_API_KEY, ""));

    reqEntity.addPart("vUserName", new StringBody(fullName));

    String userId = sharedPreferences.getString(ConstantCode.USER_ID, "");
    String accessToken = sharedPreferences.getString(ConstantCode.ACCESS_TOKEN, "");

    httppost.addHeader("X_API_KEY", x_api_key);
    httppost.addHeader("accesstoken", accessToken);
    httppost.addHeader("iUserID", userId);
    httppost.setEntity(reqEntity);

    HttpResponse response = httpclient.execute(httppost);
    str_response = EntityUtils.toString(response.getEntity());
    Log.e(TAG, "  Edit Profile response  " + str_response);

    if (!str_response.equals(""))
    {
        try
        {

            JSONObject obj = new JSONObject(str_response);

        arryResponse[0] = obj.getString("STATUS");
        arryResponse[1] = obj.getString("MESSAGE");


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

    }

    } catch (ClientProtocolException e)
    {
        Log.e(TAG, "  Edit Profile ClientProtocolException  " + e.toString());
    } catch (IOException e)
    {
        Log.e(TAG, "  Edit Profile IOException  " + e.toString());
    }
}
;

return arryResponse;
}

1 个答案:

答案 0 :(得分:0)

最后,我得到了解决这个问题的方法,很高兴:)

在这里,我使用了facebook profile pic的图像链接,并直接覆盖了这个问题的主要根源Pic,所以我将我的图像存储到sdcard并从那里获取并使用该存储图像的sdcard路径给它我脸上带着微笑。问题解决了。

根据上面的代码,只需更改路径

即可
if (profilePicPath.length() > 0)
{
    File F = new File(profilePicPath);// Path from SD-Card Like '/sdcard/example/image1.jpg'
    FileBody fb = new FileBody(F);
    reqEntity.addPart("vProfilePicture", fb);
}

if (bannerPath.length() > 0)
{
    File F = new File(bannerPath);// Path from SD-Card Like '/sdcard/example/image2.jpg'
    FileBody fb = new FileBody(F);
    reqEntity.addPart("vCoverPicture", fb);
}