2016-05-20 1 views
0

私はアンドロイドでポスト呼び出しを行うためにここにhttps://developer.android.com/reference/java/net/HttpURLConnection.htmlアンドロイドHttpURLConnect POSTストリームサイズ

をチュートリアルに従うことをしようとしています。私はそれを持っている問題私はhttp接続へのポストコールを書く方法がわかりません。

URL url = new URL("https://chart.googleapis.com/chart"); 
      HttpURLConnection client = null; 
      client = (HttpURLConnection) url.openConnection(); 
      client.setRequestMethod("POST"); 

      client.setRequestProperty("cht", "lc"); 
      client.setRequestProperty("chtt", "This is | my chart"); 
      client.setRequestProperty("chs", "300x200"); 
      client.setRequestProperty("chxt", "x"); 
      client.setRequestProperty("chd", "t:40,20,50,20,100"); 
      client.setDoOutput(true); 
      client.setChunkedStreamingMode(0); 

      OutputStream outputPost = new BufferedOutputStream(client.getOutputStream()); 
      outputPost.write(client.getRequestProperties().toString().getBytes()); 
      outputPost.flush(); 
      outputPost.close(); 

      InputStream in = new BufferedInputStream(client.getInputStream()); 
      Log.d(TAG, "Input" + in.read()); 
      client.disconnect(); 
     } catch (MalformedURLException error) { 
      //Handles an incorrectly entered URL 
      Log.d(TAG, "MalformedURL"); 
     } catch (SocketTimeoutException error) { 
//Handles URL access timeout. 
      Log.d(TAG, "Socket Timeout"); 
     } catch (IOException error) { 
//Handles input and output errors 
      Log.d(TAG, "IOexception"); 
     } 

チュートリアルでは、ストリームを書き込むためにカスタムメソッドを使用していますが、私はまだPOSTのボディのためにバイトの不明な番号を書き込むに実行します。考慮すべき

答えて

1

質問:

  1. GoogleのチャートAPIは、ヘッダ変数を経由して送信される情報を必要としていますか?
  2. GoogleのチャートAPIで情報を送信する必要がありますか?
  3. 本文の情報は正しい形式で送信されていますか?
  4. 可変Content-Typeヘッダが欠落する理由ヘッダとボディに設定されている同じデータでありますか?

Google Chart Guideを読んだ後、次のコードは、Google Chart APIにPOSTリクエストを行い、イメージをバイトとして取得します。私はそれが何だった投稿con.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));

public byte[] getImage() throws IOException { 
    URL obj = new URL("https://chart.googleapis.com/chart"); 
    HttpURLConnection con = (HttpURLConnection) obj.openConnection(); 

    // Store the post data 
    Map<String,Object> params = new LinkedHashMap<>(); 
    params.put("cht", "lc"); 
    params.put("chtt", "This is | my chart"); 
    params.put("chs", "300x200"); 
    params.put("chxt", "x"); 
    params.put("chd", "t:40,20,50,20,100"); 

    // Build the post data into appropriate format 
    StringBuilder postData = new StringBuilder(); 
    for (Map.Entry<String,Object> param : params.entrySet()) { 
     if (postData.length() != 0) postData.append('&'); 
     postData.append(URLEncoder.encode(param.getKey(), "UTF-8")); 
     postData.append('='); 
     postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8")); 
    } 

    byte[] postDataBytes = postData.toString().getBytes("UTF-8"); 

    con.setRequestMethod("POST"); 
    con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
    con.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length)); 
    con.setDoOutput(true); 

    // post the data 
    con.getOutputStream().write(postDataBytes); 

    // opens input stream from the HTTP connection 
    InputStream inputStream = con.getInputStream(); 

    // read the data from response 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    byte[] byteChunk = new byte[4096]; // Or whatever size you want to read in at a time. 
    int n; 

    while ((n = inputStream.read(byteChunk)) > 0) { 
     baos.write(byteChunk, 0, n); 
    } 

    inputStream.close(); 
    return baos.toByteArray(); 
} 
+0

感謝した後:ポストデータを書き込むには

getImageコードサンプルに次の行を参照してください。ポストのサイズを設定するには、以下の行のcon.getOutputStream().write(postDataBytes);

注意し私は、クライアントとは別のメッセージの本文を構築することを検討し始めました。 .setRequestPropertyを使用すると、ヘッダーに情報が格納されているため、ストリームへの書き込みを使用しないで接続が確立されたときに送信されます。 – user2381309

+0

情報はストリームに書き込まずに送信されますが、リクエストヘッダーの情報を検索するかどうかはGoogleのAPIに依存します... –

関連する問題