AndroidネットワークプログラミングのHttpUrlConnection-POSTリクエスト



Httpurlconnection Android Network Programming Post Request



1、サーブレット開発を使用したサーバーのバックグラウンド。ここでは紹介しません。

2、ネットワーク開発は、構成ファイルにネットワークへのアクセスを追加することを忘れないでください



3、ネットワーク要求、処理はメインスレッドでは実行できません。子スレッドで実行する必要があります。ネットワーク要求には通常約1〜3秒の遅延があるため、メインスレッドをメインスレッドで一時停止させることは、ユーザーエクスペリエンスにとって致命的です。 (メインスレッドは、ネットワークリクエスト、リソースのダウンロードなどのUIのみを描画する必要があり、さまざまな時間のかかる操作を子スレッドに配置する必要があります)。

4、



public class PostActivity extends Activity { private TextView mTvMsg private String result = null @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub  requestWindowFeature(Window.FEATURE_NO_TITLE) super.onCreate(savedInstanceState) setContentView(R.layout.activity_post) initView() } private void initView(){ mTvMsg = (TextView) findViewById(R.id.tv_msg) new Thread(postThread).start() } private Thread postThread = new Thread(){ public void run() { HttpURLConnection connection = null try { URL url = new URL('http://192.168.23.1:8080/TestProject/PostTest') connection = (HttpURLConnection) url.openConnection() // Set request method connection.setRequestMethod('POST') // Set the encoding format connection.setRequestProperty('Charset', 'UTF-8') // Pass custom parameters connection.setRequestProperty('MyProperty', 'this is me!') // Set allowable output connection.setDoOutput(true) // Upload an image FileInputStream file = new FileInputStream(Environment.getExternalStorageDirectory().getPath() + '/Pictures/Screenshots/Screenshot_2015-12-19-08-40-18.png') OutputStream os = connection.getOutputStream() int count = 0 while((count=file.read()) != -1){ os.write(count) } os.flush() os.close() // Get return data if(connection.getResponseCode() == 200){ InputStream is = connection.getInputStream() result = StringStreamUtil.inputStreamToString(is) Message msg = Message.obtain() msg.what = 0 postHandler.sendMessage(msg) } } catch (MalformedURLException e) { // TODO Auto-generated catch block  e.printStackTrace() } catch (IOException e) { // TODO Auto-generated catch block  e.printStackTrace() } finally { if(connection!=null){ connection.disconnect() } } } } private Handler postHandler = new Handler(){ public void handleMessage(android.os.Message msg) { if(msg.what==0 && result!=null){ mTvMsg.setText(result) } } } }

5、

6、アップロードJson文字列は非常に長いので、Json文字列をバイトストリームアップロードと見なすことができますが、Json文字列の長さを示すために:Json長さ+ Json +ファイルストリーム。サーバーは、Jsonの長さに基づいてJsonストリームとファイルストリームを区別できます。



7.大きなファイルをアップロードすると、クラッシュが発生する場合があります。 HttpUrlConnectionを設定できます。

// Set the stream size for each transfer, which can effectively prevent the phone from crashing due to insufficient memory. // This method is used to enable the flow of the HTTP request body without internal buffering when the content length is not known in advance. connection.setChunkedStreamingMode(51200) // 128K

8、最初のステップ:URLオブジェクトをインスタンス化します。

手順2:HttpUrlConnectionオブジェクトをインスタンス化します。

3番目のステップ:リクエスト接続プロパティ、パスパラメータなどを設定します。

ステップ4:戻りコードを取得して、リンクが成功したかどうかを判別します。

ステップ5:入力ストリームを読み取ります。

手順6:リンクを閉じます。

転載:https://www.cnblogs.com/begin1949/p/5059262.html