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服务。
客户端运行,如下,点击相应按钮,即可发送消息至服务器。