1. 程式人生 > >Andriod 手機控制 Powerpoint 演示(客戶端)

Andriod 手機控制 Powerpoint 演示(客戶端)

Andriod PPT控制客戶端主要思路

連線WCF公佈的服務,通過Request,獲取服務端資訊以及傳送資訊。

主要類

WebDataHelper--實現WCF 訪問,包含收發資料(JSON格式)

package JHKJ.homeplay;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.protocol.HTTP;
import org.json.JSONStringer;

import android.util.Log;

public class WebDataHelper {
	private static final String TAG = "WebDataGetAPI";
	private static final String USER_AGENT = "Mozilla/4.5";

	protected String getRequest(String url) throws Exception {
		return getRequest(url, new DefaultHttpClient(new BasicHttpParams()));
	}

	protected String getRequest(String url, DefaultHttpClient client)
			throws Exception {
		String result = null;
		int statusCode = 0;
		HttpGet getMethod = new HttpGet(url);
		Log.d(TAG, "do the getRequest,url=" + url + "");
		try {
			getMethod.setHeader("User-Agent", USER_AGENT);
			// HttpParams params = new HttpParams();

			// 新增使用者密碼驗證資訊
			// client.getCredentialsProvider().setCredentials(
			// new AuthScope(null, -1),
			// new UsernamePasswordCredentials(mUsername, mPassword));

			HttpResponse httpResponse = client.execute(getMethod);
			// statusCode == 200 正常
			statusCode = httpResponse.getStatusLine().getStatusCode();
			Log.d(TAG, "statuscode = " + statusCode);
			// 處理返回的httpResponse資訊
			result = retrieveInputStream(httpResponse.getEntity());
		} catch (Exception e) {
			Log.e(TAG, e.getMessage());
			throw new Exception(e);
		} finally {
			getMethod.abort();
		}
		return result;
	}

	public String Baserequest(String url,String Command)
			throws Exception {

			int statusCode = 0;
			String result = null;

			Log.i(TAG, "UTL的長度:" + url.length());
			Log.i(TAG, "URL:" + url);

			HttpClient client = new DefaultHttpClient();

			HttpPost request = new HttpPost(url);

			try {
			request.setHeader("Accept", "application/json");
			request.setHeader("Content-type", "application/json");

			JSONStringer json = new JSONStringer()
			.object()
			.key("Message").value(Command)
			.endObject();

			StringEntity entity = new StringEntity(json.toString(),"UTF-8");

			request.setEntity(entity);

			HttpResponse httpResponse = client.execute(request);

			statusCode = httpResponse.getStatusLine().getStatusCode();

			Log.i(TAG, "statusCode===============" + statusCode);

			if (statusCode == 200) {
			// 處理返回的httpResponse資訊
			result = retrieveInputStream(httpResponse.getEntity());
			}
			}
			catch(Exception e)
			{
				Log.e(TAG, e.getMessage());
			}
			return result;
	}

	/**
	 * 處理httpResponse資訊,返回String
	 * 
	 * @param httpEntity
	 * @return String
	 */
	protected String retrieveInputStream(HttpEntity httpEntity) {
		int length = (int) httpEntity.getContentLength();
		if (length < 0)
			length = 10000;
		StringBuffer stringBuffer = new StringBuffer(length);
		try {
			InputStreamReader inputStreamReader = new InputStreamReader(
					httpEntity.getContent(), HTTP.UTF_8);
			char buffer[] = new char[length];
			int count;
			while ((count = inputStreamReader.read(buffer, 0, length - 1)) > 0) {
				stringBuffer.append(buffer, 0, count);
			}
		} catch (UnsupportedEncodingException e) {
			Log.e(TAG, e.getMessage());
		} catch (IllegalStateException e) {
			Log.e(TAG, e.getMessage());
		} catch (IOException e) {
			Log.e(TAG, e.getMessage());
		}
		return stringBuffer.toString();
	}
}

WCFJsonDataHelper 對於WCF 服務訪問的封裝繼承WebDataHelper

package JHKJ.homeplay;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;



public class WCFJsonDataHelper extends WebDataHelper {
	private static String BASE_URL = "";
	public static String getBASE_URL() {
		return "http://"+BASE_URL+":8089/";
	}

	public static void setBASE_URL(String bASE_URL) {
		BASE_URL = bASE_URL;
	}

	private static final String EXTENSION = "Json/";;


	public JSONObject getObject(String sbj) throws JSONException, Exception {
		return new JSONObject(getRequest(getBASE_URL() + EXTENSION + sbj));
	}

	public String getString(String sbj) throws JSONException, Exception {
		return new String(getRequest(getBASE_URL() + EXTENSION + sbj).toString());
	}

	public JSONArray getArray(String sbj) throws JSONException, Exception {
		return new JSONArray(getRequest(getBASE_URL() + EXTENSION + sbj));
	}

	public String SendCommand(String sbj, String CommandString)
			throws JSONException, Exception {
		return new String(Baserequest((getBASE_URL() + EXTENSION + sbj),
				CommandString).toString());
	}

}

HomePlayActivity --介面邏輯,也就定義幾個按鈕,呼叫上面的WCF通訊類,傳送訊息而已。
package JHKJ.homeplay;

import org.json.JSONException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class HomePlayActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		if (InitSetting()) {
			getJsonData();
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.new_game:
			Intent intent = new Intent();  
		    intent.setClass(HomePlayActivity.this, settingActivity.class);  
		    HomePlayActivity.this.startActivity(intent);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private Boolean InitSetting() {
		// TODO Auto-generated method stub
		SharedPreferences sp = getSharedPreferences("preferences",
				Context.MODE_PRIVATE);
		String DefautlServer = sp.getString("Server", "");
		if (DefautlServer == "") {
			Toast toast = Toast.makeText(getApplicationContext(),
					"伺服器地址為空,請配置伺服器!", Toast.LENGTH_LONG);// 提示被點選了
			toast.show();
			return false;

		} else {

			WCFJsonDataHelper.setBASE_URL(DefautlServer);
			return true;
		}

	}

	Button btnPrevButton;
	Button btnNextButton;
	Button btnFristButton;
	Button btnLastButton;

	public void getJsonData() {
		WCFJsonDataHelper api = new WCFJsonDataHelper();

		try {
			// 呼叫GetAccountData方法
			String stateString = api.getString("GetServerState");

			((TextView) findViewById(R.id.ServerState)).setText(stateString);

			btnFristButton = (Button) this.findViewById(R.id.btnFirst);
			btnFristButton.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					WCFJsonDataHelper api = new WCFJsonDataHelper();
					try {

						api.SendCommand("SendMessageJson", "First");
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					Toast toast = Toast.makeText(getApplicationContext(),
							"已發訊息", Toast.LENGTH_LONG);// 提示被點選了
					toast.show();

				}
			});

			btnLastButton = (Button) this.findViewById(R.id.btnLast);
			btnLastButton.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					WCFJsonDataHelper api = new WCFJsonDataHelper();
					try {

						api.SendCommand("SendMessageJson", "Last");
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					Toast toast = Toast.makeText(getApplicationContext(),
							"已發訊息", Toast.LENGTH_LONG);// 提示被點選了
					toast.show();

				}
			});

			btnPrevButton = (Button) this.findViewById(R.id.btnPerv);
			btnPrevButton.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					WCFJsonDataHelper api = new WCFJsonDataHelper();
					try {

						api.SendCommand("SendMessageJson", "Perv");
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					Toast toast = Toast.makeText(getApplicationContext(),
							"已發訊息", Toast.LENGTH_LONG);// 提示被點選了
					toast.show();

				}
			});

			btnNextButton = (Button) this.findViewById(R.id.btnNext);
			btnNextButton.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					WCFJsonDataHelper api = new WCFJsonDataHelper();
					try {

						api.SendCommand("SendMessageJson", "Next");
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					Toast toast = Toast.makeText(getApplicationContext(),
							"已發訊息", Toast.LENGTH_LONG);// 提示被點選了
					toast.show();

				}
			});

		} catch (Exception e) {
			Toast.makeText(getApplicationContext(), e.getMessage(),
					Toast.LENGTH_LONG).show();
			e.printStackTrace();
		}
	}
}

客戶端這邊講起來比較雜亂,核心程式碼就是上面這些。

單執行PPT演示時候,外掛即可執行啟動WCF服務。


客戶端執行,如下,點選相應按鈕,即可傳送訊息至伺服器。