オープンソース・ソフトウェアの開発とダウンロード

Subversion リポジトリの参照

Contents of /trunk/OpenSangokushi/src/jp/or/cute/sangokushi/httpclient/BattleCommand.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 256 - (show annotations) (download)
Mon May 10 01:25:31 2010 UTC (14 years ago) by izumist
File MIME type: text/plain
File size: 5526 byte(s)


1 package jp.or.cute.sangokushi.httpclient;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.InputStreamReader;
7 import java.io.OutputStreamWriter;
8 import java.net.HttpURLConnection;
9 import java.net.URL;
10 import java.net.URLEncoder;
11 import java.util.Iterator;
12
13 import android.util.Log;
14
15 /*
16 * ルーム退室
17 * @author iST
18 * @version 0.1
19 * @since 2010/05/04
20 */
21 public class BattleCommand extends HttpCommand{
22 protected final static String BASE_PATH = "http://sangokushidev.cute.or.jp/index.php";
23 protected final static int BATTLE_COMMAND_ID = 0;
24
25 private String mUserid = "U7bd5r6a2d";
26 private String mRoomid;
27
28 // 登録されているコマンド(未送信)
29 // 送信されると0になる
30 private int commandCount = 0;
31 private String[] command = new String[2];
32
33 public void MoveCell(int x,int y)
34 {
35 int cell = y * 45 + x + 1;
36 String cmd = "movemapchipno=" + Integer.toString(cell);
37 cmd += "&userid=" + this.mUserid;
38 cmd += "&roomid=" + this.mRoomid;
39 Log.d("BattleCommand","["+Integer.toString(commandCount)+"]"+cmd);
40 String result = httpPost(cmd);
41 }
42
43 public void BattlePlayer(int x,int y) {
44 int cell = y * 45 + x + 1;
45 String cmd = "battlemapchipno="+Integer.toString(cell);
46 cmd += "&battlecommandid="+Integer.toString(this.BATTLE_COMMAND_ID);
47 cmd += "&userid=" + this.mUserid;
48 cmd += "&roomid=" + this.mRoomid;
49 Log.d("BattleCommand","["+Integer.toString(commandCount)+"]"+cmd);
50 String result = httpPost(cmd);
51 }
52
53 public String httpGet(String location){
54 HttpURLConnection http = null;
55 InputStream in = null;
56 URL url = null;
57 String response = null;
58 ByteArrayOutputStream baos = new ByteArrayOutputStream();
59 try {
60 // 指定した URL の作成
61 String urlString = BASE_PATH + "?" + location;
62 Log.d("httpget","urlString="+urlString);
63 url = new URL(urlString); //location);
64 // HTTP通信の初期化
65 http = (HttpURLConnection) url.openConnection();
66 // HTTP通信のメソッド指定(今回は取得のみなので GET を指定)
67 http.setRequestMethod("GET");
68 // UserAgentのセット
69 //http.setRequestProperty("User-Agent", "Android Application");
70 // HTTP通信開始
71 http.connect();
72 Log.d("httpget","status="+Integer.toString(http.getResponseCode()));
73 // HTTP通信でデータを取得
74 in = http.getInputStream();
75 int len;
76 byte[] buf = new byte[4096];
77 while ( (len = in.read(buf))> 0) {
78 baos.write(buf, 0, len);
79 }
80 Log.d("httpget","status="+Integer.toString(http.getResponseCode()));
81 // 取得したデータを Stringへ変換
82 response = new String(baos.toByteArray());
83 } catch (Exception e) {
84 e.printStackTrace();
85 } finally{
86 // HTTP 通信の後始末
87 try{
88 in.close();
89 } catch (Exception e) {}
90 try {
91 http.disconnect();
92 } catch (Exception e) {}
93 }
94 Log.d("BattleCommand","response="+response);
95 return response;
96 }
97
98
99
100 public String httpPost(String cmd) {
101 String result = "";
102 HttpURLConnection conn = null;
103 String postdata = "";
104
105 // post送信データを1本の文字列にまとめる
106 try {
107 URL url = new URL(BASE_PATH);
108 Log.d("BattleCommand", "cmd=\"" + cmd +"\"");
109
110 conn = (HttpURLConnection)url.openConnection();
111 conn.setReadTimeout(10*1000 /* m秒 */);
112 conn.setConnectTimeout(15*1000 /* m秒 */);
113 conn.setRequestMethod("POST");
114 conn.setDoInput(true);
115 conn.setDoOutput(true);
116 if (cmd.length() > 0) {
117 OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(),"UTF-8");
118 writer.write(cmd);
119 writer.flush();
120 writer.close();
121 }
122 conn.connect();
123 InputStreamReader reader = new InputStreamReader(conn.getInputStream(),"UTF-8");
124 while (true) {
125 char[] buffer = new char[4096]; // #chars <= #bytes
126 int nchars = reader.read(buffer);
127 if (nchars < 0)
128 break;
129 result += String.valueOf(buffer, 0, nchars);
130 }
131 reader.close();
132 Log.d("BattleCommand","result="+result);
133 } catch (IOException e) {
134 result = null;
135 Log.d("BattleCommand","Error message="+e.getMessage());
136 } finally {
137 if (conn != null) conn.disconnect();
138 }
139 return result;
140 }
141
142 public BattleCommand(String sessionID) {
143 super(POST,sessionID);
144 this.setPath(HttpClient.BASE_PATH);
145 commandCount = 0;
146 this.setParam(HttpClient.PARAMS_COMMAND,HttpClient.COMMAND_SET_BATTLE);
147 }
148
149
150 public BattleCommand(String userid,String roomid,String sessionID) {
151 super(POST,sessionID);
152 mUserid = userid;
153 mRoomid = roomid;
154 this.setPath(HttpClient.BASE_PATH);
155 commandCount = 0;
156 this.setParam(HttpClient.PARAMS_COMMAND,HttpClient.COMMAND_SET_BATTLE);
157 }
158
159 public BattleCommand(String method,String sessionID) {
160 super(POST,sessionID);
161 this.setPath(HttpClient.BASE_PATH);
162 commandCount = 0;
163 this.setParam(HttpClient.PARAMS_COMMAND,HttpClient.COMMAND_SET_BATTLE);
164 }
165 }
166
167

Properties

Name Value
svn:mime-type text/plain

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26