【问题标题】:File not found exception in upload image on server using file body使用文件正文在服务器上上传图像时找不到文件异常
【发布时间】:2014-07-16 13:51:40
【问题描述】:

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

谁能帮我摆脱困境。

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

【问题讨论】:

  • 发布Logcat及相关代码。这么难理解吗?
  • 请原谅投反对票的人,如果您不知道答案,请不要碰问题,请留下。如果您对此有所了解,请不要通过投票来展示您的伟大,请给出答案并证明您的知识。
  • @Marius 我把我的代码,可以帮助我吗?
  • 也包括Logcat,并高亮出现异常的部分。我对你投了反对票,因为问题几乎没有显示任何研究,也没有足够的细节来识别问题。如果你解决了这些问题,我将删除我的反对票。

标签: android exception http-post


【解决方案1】:

终于,我得到了这个问题的解决方案,我很高兴:)

在这里,我使用了 facebook 个人资料图片的图片链接,并直接覆盖图片,这是这个问题的主要根源,所以我将我的图片存储到 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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-16
    • 1970-01-01
    • 2016-06-06
    • 2019-02-20
    • 2012-11-16
    • 2020-04-29
    • 1970-01-01
    相关资源
    最近更新 更多