今天算是有點小激動呢!拿到Android與PHP這個課題已經兩個星期了,直到今天才算是有了一點點小收獲。
雖然還是沒能成功上傳到服務器,不過已經看到了曙光,已經實現了一半了,那就是已經連接到了服務器。不
說廢話了,還是寫點自己的記錄吧!
如果想要實現Android與PHP的交互我們首先應該知道一個叫AsynTask的抽象類。
下面是我的筆記:
為了方便在子線程中對UI進行操作,Android提供了一些好用煩人工具類,AsynTask就是其中之一。借助AsynTask
,可以十分簡單地從子線程切換到主線程,它的原理也是基於異步消息處理機制的。
AsynTask的基本用法:
1.AsynTask是一個抽象類,因此使用它必須要創建一個類去繼承它。在繼承AsynTask時,可以為其指定三個泛型
參數,這三個參數的用途如下所示:
Params:在執行AsynTask時需要傳入的參數,用於后台的任務中使用。
Progress:后台任務執行時,如果需要在界面上顯示當前的進度,則使用該參數作為進度單位。
Result:當任務執行完畢后,如果需要對結果進行返回,則使用該參數作為返回值類型。
例如:class DownLoadTask extends AsynTask<void,Integer,Boolean>{
...
}
通常使用AsynTask時,需要重寫它的4個方法。
1.onPreExcute():這個方法在后台任務執行之前調用,一般用於界面上初始化操作,例如,顯示一個
進度條對話框等。
2.doInBackground(Params...):這個方法在子線程中運行,用於處理耗時操作,操作一旦完既可以
通過return語句將任務的執行結果返回。如果AsynTask的第三個泛型參數指定的是void則可以不用返回
結果。需要注意的是,這個方法不能進行更新UI操作,如果要在該方法中更新UI可以手動調動
publishProgress(Progress...)方法來完成。
3.onProgressUpdate(Progress...):如果在doInBackground(Params...)方法中調用了publishProgress
(Progress...)方法,這個方法就會很快被調用,方法中攜帶的參數就是后台任務中傳遞過來的
在這個方法可以對UI進行操作,利用參數Progress就可以對UI進行相應的更新。
4.onPostExcute(Result):當doInBackground(Params...)執行完畢通過return語句進行返回時,這個
方法會很快被調用。在doInBackground(Params...)中返回的數據會作為參數傳遞到該方法中。此時
可以利用返回的參數來進行UI操作,例如,提醒某個任務完成了。
package com.itcast.asyntask;
import android.os.AsyncTask;
import android.widget.Toast;
public class DownLoadTask extends AsyncTask<Void,Integer,Boolean>{
protected void onPreExecute(){
progressDialog.show();
}
@Override
protected Boolean doInBackground(Void... params) {
// TODO Auto-generated method stub
while(true){
int downloadPrecent = doDownload();
publishProgress(downloadPrecent);
if(downloadPrecent >= 100){
break;
}
}
return true;
}
protected void onProgressUpdate(Integer...values){
progressDialog.setMessage("Download"+values[0]+"%");
}
protected void onPostExecute(Boolean result){
progressDialog.dismiss();
if(result){
Toast.makeText(MainActivity.this, "下載成功", 0).show();
}else{
Toast.makeText(MainActivity.this, "下載失敗", 0).show();
}
}
}
要執行DownLoadTask,還需要在UI線程中創建出DownLoadTask的實例,並調用DownLoadTask
實例的Excute()方法。代碼如下:
new DownLoadTask().excute();
接下來是我的一個交互的小程序:
步驟為:1.寫布局文件 2.寫主程序 3.添加網絡訪問權限
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context=".MainActivity" > 10 11 <TextView 12 android:id="@+id/textView1" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:textSize="20sp" 16 android:text="上傳文件" /> 17 18 <TextView 19 android:id="@+id/textView2" 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:layout_below="@+id/textView1" 23 android:text="TextView" /> 24 25 <Button 26 android:id="@+id/button1" 27 android:layout_width="wrap_content" 28 android:layout_height="wrap_content" 29 android:layout_below="@+id/textView2" 30 android:text="確定上傳" /> 31 32 33 </RelativeLayout>
package com.itcastupfile; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import android.os.AsyncTask; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private String uploadFile = "/mnt/sdcard/DCIM/Camera/IMG_20160406_114143.JPG"; private String srcPath = "/mnt/sdcard/DCIM/Camera/IMG_20160406_114143.JPG"; private String actionUrl = "http://10.6.78.90/xampp/sse/index.php/home/Index/upload_file"; private TextView mText1; private TextView mText2; private Button mButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mText1 = (TextView)findViewById(R.id.textView1); mText1.setText("文件路徑:\n"+uploadFile); mText2 = (TextView)findViewById(R.id.textView2); mText2.setText("上傳網址:\n"+actionUrl); mButton = (Button)findViewById(R.id.button1); mButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub uploadFile(actionUrl); //FileUploadTask fileuploadtask = new FileUploadTask(); //fileuploadtask.execute(); } }); } private void uploadFile(String actionUrl) { // TODO Auto-generated method stub String end = "\r\n"; String twoHyphens = "--"; String boundary = "******"; try{ URL url = new URL(actionUrl); HttpURLConnection httpURLConnection = (HttpURLConnection) url .openConnection(); // 設置每次傳輸的流大小,可以有效防止手機因為內存不足崩潰 // 此方法用於在預先不知道內容長度時啟用沒有進行內部緩沖的 HTTP 請求正文的流。 httpURLConnection.setChunkedStreamingMode(128 * 1024);// 128K // 允許輸入輸出流 httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(true); httpURLConnection.setUseCaches(false); // 使用POST方法 httpURLConnection.setRequestMethod("POST"); httpURLConnection.setRequestProperty("Connection", "Keep-Alive"); httpURLConnection.setRequestProperty("Charset", "UTF-8"); httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); DataOutputStream dos = new DataOutputStream( httpURLConnection.getOutputStream()); dos.writeBytes(twoHyphens + boundary + end); dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"" + srcPath.substring(srcPath.lastIndexOf("/") + 1) + "\"" + end); dos.writeBytes(end); FileInputStream fis = new FileInputStream(srcPath); byte[] buffer = new byte[8192]; // 8k int count = 0; // 讀取文件 while ((count = fis.read(buffer)) != -1) { dos.write(buffer, 0, count); } fis.close(); dos.writeBytes(end); dos.writeBytes(twoHyphens + boundary + twoHyphens + end); dos.flush(); InputStream is = httpURLConnection.getInputStream(); InputStreamReader isr = new InputStreamReader(is, "utf-8"); BufferedReader br = new BufferedReader(isr); String result = br.readLine(); Toast.makeText(this, result, Toast.LENGTH_LONG).show(); dos.close(); is.close(); } catch (Exception e) { e.printStackTrace(); setTitle(e.getMessage()); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
<uses-permission android:name="android.permission.INTERNET" />
下面是我的運行結果:
看到的那個Toast就是PHP端的代碼,判斷是否上傳成功!
不過此程序仍然存在問題就是無法顯示上傳成功,問題還在查詢當中。
不過已經算是實現交互的功能了。接下來就是查找上傳不成功的原因了。