2015-12-04 108 views
1

我想用我的JSONParser類上傳圖像。 這裏是類:使用JSonParser將圖像上傳到PHP服務器

public class JSONParser { 

    String charset = "UTF-8"; 
    HttpURLConnection conn; 
    DataOutputStream wr; 
    StringBuilder result = new StringBuilder(); 
    URL urlObj; 
    JSONObject jObj = null; 
    StringBuilder sbParams; 
    String paramsString; 

    public JSONObject makeHttpRequest(final Context context,String url, String method, 
             HashMap<String, String> params) { 

     sbParams = new StringBuilder(); 
     int i = 0; 
     for (String key : params.keySet()) { 
      try { 
       if (i != 0){ 
        sbParams.append("&"); 
       } 
       sbParams.append(key).append("=") 
         .append(URLEncoder.encode(params.get(key), charset)); 

      } catch (UnsupportedEncodingException e) { 
       e.printStackTrace(); 
      } 
      i++; 
     } 

     if (method.equals("POST")) { 
      // request method is POST 
      try { 
       urlObj = new URL(url); 

       conn = (HttpURLConnection) urlObj.openConnection(); 

       conn.setDoOutput(true); 

       conn.setRequestMethod("POST"); 

       conn.setRequestProperty("Accept-Charset", charset); 


       conn.setReadTimeout(10*1000);//orijinal 10000 

       conn.setConnectTimeout(15*1000);//orijinal 15000 

       conn.connect(); 

       paramsString = sbParams.toString(); 

       wr = new DataOutputStream(conn.getOutputStream()); 
       wr.writeBytes(paramsString); 
       wr.flush(); 
       wr.close(); 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     else if(method.equals("GET")){ 
      // request method is GET 

      if (sbParams.length() != 0) { 
       url += "?" + sbParams.toString(); 
      } 

      try { 
       urlObj = new URL(url); 

       conn = (HttpURLConnection) urlObj.openConnection(); 

       conn.setDoOutput(false); 

       conn.setRequestMethod("GET"); 

       conn.setRequestProperty("Accept-Charset", charset); 

       conn.setConnectTimeout(15000); 

       conn.connect(); 

      } catch (IOException e) { 
       MyMethods.makeToastInAsync(context,context.getResources().getString(R.string.connProblem),Toast.LENGTH_LONG); 
       e.printStackTrace(); 
      } 

     } 

     try { 
      //Receive the response from the server 
      InputStream in = new BufferedInputStream(conn.getInputStream()); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 

      String line; 
      while ((line = reader.readLine()) != null) { 
       result.append(line); 
      } 

      Log.d("JSON Parser", "result: " + result.toString()); 

     } catch (IOException e) { 

      MyMethods.makeToastInAsync(context,context.getResources().getString(R.string.connProblem),Toast.LENGTH_LONG); 

      e.printStackTrace(); 
     } 

     conn.disconnect(); 

     // try parse the string to a JSON object 
     try { 
      jObj = new JSONObject(result.toString()); 
     } catch (JSONException e) { 
      Log.e("JSON Parser", "Error parsing data " + e.toString()); 
     } 

     // return JSON Object 
     return jObj; 
    } 
} 

SendImage類的異步部分:

protected JSONObject doInBackground(String... args) { 

     try { 

      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
      image.compress(Bitmap.CompressFormat.JPEG, 50, byteArrayOutputStream); 
      String encodedImage = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT); 

      Log.d("Send Image", encodedImage); 


      HashMap<String, String> dataTosend = new HashMap<>(); 
      dataTosend.put("image", encodedImage); 
      dataTosend.put("name", sharedPreferences.getString("username", "")); 

      JSONObject json = jsonParser.makeHttpRequest(SendImage.this, 
        UPLOAD_IMAGE_URL, "POST", dataTosend); 

      if (json != null) { 
       Log.d("JSON result", json.toString()); 

       return json; 
      } 


     }catch (Exception e){ 
      e.printStackTrace(); 
     } 

     return null; 
    } 

所以,你看,我首先壓縮的圖像和Base64編碼,並返回字符串與JSONParser發。

的問題是,我可以送形象,沒有那麼大,但我不能發送,如果它超過6 MB(約)。我得到這個錯誤:在line

java.io.FileNotFoundException: http://example.com/uploadImage.php 

和錯誤:

InputStream in = new BufferedInputStream(conn.getInputStream()); 
(E/JSON Parser: Error parsing data org.json.JSONException: End of input at character 0 of) 

在我看來,我得到JSON異常,因爲沒有從服務器的答案。但我不明白爲什麼我得到這個錯誤。

java.io.FileNotFoundException: http://example.com/uploadImage.php 

是否有可能,該服務器不接受傳入文件超過某些Mb?

編輯:當我將壓縮比從50改爲10時,我可以上傳圖像,我不能。所以,我確信由於圖像大小而發生錯誤。前setReadTimeoutsetConnectTimeout超過發生

此錯誤。

我的PHP代碼:

<?php 
if (!empty($_POST)) { 


$name =$_POST["name"]; 
$image =$_POST["image"]; 

$decodedImage= base64_decode("$image"); 

$resultt=file_put_contents("images/" .$name. ".JPG",$decodedImage); 

if($resultt){ 


    $response["success"] = 1; 
     $response["message"] = "".$resultt. ""; 
     die(json_encode($response)); 

} 
    $response["success"] = 0; 
     $response["message"] = "Fail"; 
     die(json_encode($response)); 

} 



?> 

回答

0

在服務器也許問題。

php.ini文件在您的服務器內。

PHP有幾個配置選項來限制腳本消耗的資源。默認情況下,PHP設置爲允許上傳2MB或更小的文件。

嘗試在php.ini中增加了以下值,例如:

memory_limit = 32M 
upload_max_filesize = 24M 
post_max_size = 32M 
+0

我會嘗試這一點,並寫在這裏。所以,如果問題是我如何從服務器返回答案?我想將服務器的答案傳遞給JSON。 – eren130