編輯:關於Android編程
最近在學習Android網絡編程的知識,以下是今晚學習的一些心得。與大家共同分享。
package com.lee.nethttp; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.util.Base64; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { private EditText mEditText = null; private Button mButton = null; private TextView mTextView = null; private String mUrl = http://192.168.0.109:8080/lee/index.jsp;//根據自己主機的ip設置 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mEditText = (EditText) findViewById(R.id.edit); mButton = (Button) findViewById(R.id.button); mButton.setOnClickListener(mButtonClickListener); mTextView = (TextView) findViewById(R.id.text); } private OnClickListener mButtonClickListener = new OnClickListener() { @Override public void onClick(View v) { String content = mEditText.getText().toString(); content = toBase64(content); String[] params = { content }; new NetHttpAsyncTask().execute(params); } }; private class NetHttpAsyncTask extends AsyncTask{ @Override protected String doInBackground(String... params) { String result = null; try { URL url = new URL(mUrl); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod(POST); connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); connection.setRequestProperty(Content-type, application/x-www-form-urlencoded;charset=UTF-8); connection.setConnectTimeout(3000); connection.setReadTimeout(3000); PrintWriter writer = new PrintWriter(connection.getOutputStream()); String str = content + = + params[0]; writer.print(str); writer.flush(); writer.close(); InputStream input = connection.getInputStream(); ByteArrayOutputStream outputBuffer = new ByteArrayOutputStream(); int ch; while((ch = input.read()) != -1){ outputBuffer.write(ch); } result = outputBuffer.toString(); } catch (IOException e) { e.printStackTrace(); } return result; } @Override protected void onPostExecute(String result) { mTextView.setText(result); } } private String toBase64(String content){ try { content = Base64.encodeToString(content.getBytes(UTF-8), Base64.DEFAULT); Log.e(lee, toBase64 content = + content); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return content; } }
在Android開發過程中,經常會碰到Activity之間的切換效果的問題,下面介紹一下如何實現左右滑動的切換效果,首先了解一下Activity切換的實現,從Androi
很多Android手機用戶都喜歡收集APK安裝包並保存在存儲卡內。可是,網上下載的APK大都以英文或亂碼命名,管理起來好不麻煩。那麼,無需借助PC,我們能否
直接使用線程在Android開發的時候,當我們需要完成一個耗時操作的時候,通常會新建一個子線程出來,例如如下代碼new Thread(new Runnable() {
眾所周知Android的Tab控件不是很好用,因此Github上的PagerSlidingTabStrip項目被廣為使用,該項目地址為: 其示例圖如下:由於其d