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のボディのためにバイトの不明な番号を書き込むに実行します。考慮すべき
感謝した後:ポストデータを書き込むには
が
getImage
コードサンプルに次の行を参照してください。ポストのサイズを設定するには、以下の行のcon.getOutputStream().write(postDataBytes);
注意し私は、クライアントとは別のメッセージの本文を構築することを検討し始めました。 .setRequestPropertyを使用すると、ヘッダーに情報が格納されているため、ストリームへの書き込みを使用しないで接続が確立されたときに送信されます。 – user2381309
情報はストリームに書き込まずに送信されますが、リクエストヘッダーの情報を検索するかどうかはGoogleのAPIに依存します... –