2016-08-25 17 views
-2

PHPを使用してアンドロイドからJSONObjectとしてJSONObjectとしてイメージをアップロードしようとしています(base64で圧縮されています)。メソッドポストでHttpURLConnectionを使用しようとしました。イメージをjson(base64で圧縮)からアンドロイドからWebサービスphpにアップロード

私のコードは、アンドロイドにあります。

私が持っているLogcatで
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を使うことをお勧めします。volley home page

関連する問題