2016-08-25 2 views
-2

PHP를 사용하여 안드로이드에서 웹 서비스로 JSONObject로 이미지 (base64로 압축 된 이미지)를 업로드하려고합니다. 메서드 게시와 함께 HttpURLConnection을 사용하려고했습니다.안드로이드에서 웹 서비스 PHP로 이미지 업로드 json (base64로 압축)

내 코드, android.

내가 가진 로그 캣에서
try { 

      JSONObject params = new JSONObject(); 
      params.put("nombre", name); 
      params.put("img", imgBase64); 

      Log.d("params", params.toString()); 


      URL url = new URL(urlServer); 
      HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
      conn.setRequestMethod("POST"); 
      //conn.setReadTimeout(5000); 
      conn.setDoInput(true); 
      conn.setDoOutput(true); 
      conn.setRequestProperty("Content-Type", "application/json;charset=utf-8"); 



      BufferedOutputStream BuffOut = new BufferedOutputStream(conn.getOutputStream()); 
      BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(BuffOut, "UTF-8")); 
      writer.write(params.toString()); 
      writer.close(); 
      BuffOut.close(); 


      //open 
      conn.connect(); 

      //do somehting with response 
      int responseCode = conn.getResponseCode(); 

      if (responseCode == HttpURLConnection.HTTP_OK) { 
       BufferedReader buffr = new BufferedReader(new 
         InputStreamReader(
         conn.getInputStream())); 

       StringBuffer sb = new StringBuffer(""); 
       String line = ""; 

       while ((line = buffr.readLine()) != null) { 
        sb.append(line); 
        break; 
       } 
       Log.d("leer", sb.toString()); 
       buffr.close(); 
       conn.disconnect(); 

       return sb.toString(); 

      } else { 
       Log.d("err","false : " + responseCode); 
       return "false : " + responseCode; 
      } 
     } catch (JSONException e) { 
      return " Exception JSON: " + e.getMessage(); 
     } catch (IllegalStateException e) { 
      return " IllegalStateException: " + e.getMessage(); 
     } catch (UnsupportedEncodingException e) { 
      return " UnsupportedEncodingException: " + e.getMessage(); 
     } catch (ProtocolException e) { 
      return " ProtocolException: " + e.getMessage(); 
     } catch (MalformedURLException e) { 
      return " MalformedURLException: " + e.getMessage(); 
     } catch (IOException e) { 
      return " Exception IO: " + e.getMessage(); 
     } catch (Exception e) { 
      return " Exception: " + e.getMessage(); 
     } 

같은 오류 : 내가 가지고있는 서버 측에서 스트림

의 예기치 않은 종료, PHP : ProtocolException 사용

header('Content-type: application/json'); 

if($_SERVER['REQUEST_METHOD'] == "POST"){ 

    if(!empty($_POST['nombre'])&&!empty($_POST['img'])){ 
     echo json_encode(array('status' => 'ok', 'msg' => "hi ".$_POST['nombre']. " img ".$_POST['img'])); 
    } 
} 
+0

'BufferedWriter reader'??? 네 이름이 괜찮다고 생각하니? – greenapps

+0

LogCat을 해당 예외를보고 싶은대로 게시하십시오. – greenapps

+0

'BufferedOutputStream inB'? 에서? – greenapps

답변

0

특정 기능을 요구하지 않는 경우 또는 몇 가지 제약이 있습니다. 네트워크 처리를 위해 발리를 사용할 것을 권합니다. volley home page

관련 문제