2014-03-30 52 views
1

我已经阅读了很多帖子来实现上传图像以及使用android中的multipartentity和josn参数的任务,但我的问题是当我试图上传图像和无需转换字符串JSONobject参数,然后图像上传没有错误,但是当我尝试添加响应字符串到JSONObject的再发生类似的logcat的)错误:上传图像到服务器使用multipartentity和json在android

error: Value`<form of type java.lang.String cannot be converted to JSONObject. 

任何一个请帮助解决这个问题?我想发送一个图像和JsonObjec t到一个PHP服务器与MultipartEntity。我正在开发一个应用程序,允许用户使用H ttpPost方法上传图像。我使用MultipartEntity,因此我将库apache-mime4j-0.6.1.jar,httpclient-4.3.1.jar,httpcore-4.3.1.jar和httpmime-4.2.1.jar添加到我的应用程序中。

这里是我的代码:

public JSONObject doFileUpload(String _fname, String _lname, String _email, 
       String _password, String _country, String _countrycode, 
       String _phone) { 

      File file1 = new File(selectedPath); 
      String urlString = "http://capstonehostingservices.com/fetch_new/app/index.php/fetch/register"; 
      try { 
       HttpClient client = new DefaultHttpClient(); 
       HttpPost post = new HttpPost(urlString); 
       FileBody bin1 = new FileBody(file1); 
       MultipartEntity reqEntity = new MultipartEntity(); 
       reqEntity.addPart("photo", bin1); 
       reqEntity.addPart("first_name", new StringBody(_fname)); 
       reqEntity.addPart("last_name", new StringBody(_lname)); 
       reqEntity.addPart("email", new StringBody(_email)); 
       reqEntity.addPart("password", new StringBody(_password)); 
       reqEntity.addPart("country", new StringBody(_country)); 
       reqEntity.addPart("country_code", new StringBody(_countrycode)); 
       reqEntity.addPart("phone", new StringBody(_phone)); 

       post.setEntity(reqEntity); 
       HttpResponse response = client.execute(post); 
       resEntity = response.getEntity(); 
       String response_str = EntityUtils.toString(resEntity); 



        json = new JSONObject(response_str); 


      } catch (Exception ex) { 
       Log.e("Debug", "error: " + ex.getMessage(), ex); 
      } 
      return json; 
     } 

在上面的代码中,我试图响应字符串转换的JSONObject,如何我做到这一点? 我使用selectedpath参数作为从gallary得到图像路径,我想发送一个图像和一个JsonObjectMultipartEntity的PHP服务器。我正在开发一个应用程序,允许用户使用HttpPost方法上传图像。我使用MultipartEntity,因此我将库apache-mime4j-0.6.1.jar,httpclient-4.3.1.jar,httpcore-4.3.1.jar和httpmime-4.2.1.jar添加到我的应用程序中。

回答

2

这可能不是最好的答案,但它是一个很好的入门例子。

使用下面的函数发送文件到服务器:

public static void postFile(String fileName) throws Exception {//fileName is path+filename of picture 
     String url_upload_image = "http://url-to-api/upload_photo.php"; 
     HttpClient client = new DefaultHttpClient(); 
     HttpPost post = new HttpPost(url_upload_image); 
     MultipartEntityBuilder multipartEntity = MultipartEntityBuilder 
       .create(); 
     multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); 
     multipartEntity.addPart("file", new FileBody(new File(fileName))); 
     post.addHeader("id", id);//id is anything as you may need 
     post.setEntity(multipartEntity.build()); 
     HttpResponse response = client.execute(post); 
     HttpEntity entity = response.getEntity(); 

     entity.consumeContent(); 
     client.getConnectionManager().shutdown(); 
    } 

PHP文件,该文件是在http://url-to-api/upload_photo.php

<?php 
    $name = $_POST['id']; 

    $file_path = "images/"; 

    $file_path = $file_path . basename($_FILES['file']['name']); 

    if(move_uploaded_file($_FILES['file']['tmp_name'], $file_path)) { 
     echo "success"; 
     echo $name; 
    } else{ 
     echo "fail"; 
    } 
?> 

,并确保你的服务器目录或文件夹是可执行的,可写,可读。我曾将此作为主要问题。这是,称为777权限。相信我,这和其他要考虑的事情一样重要。

相关问题