所以我一直在编写一些Lua脚本,当我使用表格时,我想做一些类似于“节点”或“类”的东西
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new APIRequest().execute();
}
private class APIRequest extends AsyncTask<Void, Void, String> {
@Override
protected Object doInBackground(Void... params) {
// Of course, you should comment the other CASES when testing one CASE
// CASE 1: For FromBody parameter
String url = "http://10.0.2.2/api/frombody";
String requestBody = Utils.buildPostParameters("'FromBody Value'"); // must have '' for FromBody parameter
HttpURLConnection urlConnection = null;
try {
urlConnection = (HttpURLConnection) Utils.makeRequest("POST", url, null, "application/json", requestBody);
InputStream inputStream;
// get stream
if (urlConnection.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST) {
inputStream = urlConnection.getInputStream();
} else {
inputStream = urlConnection.getErrorStream();
}
// parse stream
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String temp, response = "";
while ((temp = bufferedReader.readLine()) != null) {
response += temp;
}
return response;
} catch (IOException e) {
e.printStackTrace();
return e.toString();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
// CASE 2: For JSONObject parameter
String url = "http://10.0.2.2/api/testjsonobject";
JSONObject jsonBody;
String requestBody;
HttpURLConnection urlConnection;
try {
jsonBody = new JSONObject();
jsonBody.put("Title", "BNK Title");
jsonBody.put("Author", "BNK");
jsonBody.put("Date", "2015/08/08");
requestBody = Utils.buildPostParameters(jsonBody);
urlConnection = (HttpURLConnection) Utils.makeRequest("POST", url, null, "application/json", requestBody);
...
// the same logic to case #1
...
return response;
} catch (JSONException | IOException e) {
e.printStackTrace();
return e.toString();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
// CASE 3: For form-urlencoded parameter
String url = "http://10.0.2.2/api/token";
HttpURLConnection urlConnection;
Map<String, String> stringMap = new HashMap<>();
stringMap.put("grant_type", "password");
stringMap.put("username", "username");
stringMap.put("password", "password");
String requestBody = Utils.buildPostParameters(stringMap);
try {
urlConnection = (HttpURLConnection) Utils.makeRequest("POST", url, null, "application/x-www-form-urlencoded", requestBody);
...
// the same logic to case #1
...
return response;
} catch (Exception e) {
e.printStackTrace();
return e.toString();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
}
@Override
protected void onPostExecute(String response) {
super.onPostExecute(response);
// do something...
}
}
这只是我正在做的一个例子,但是可以访问这样的信息吗?还是有更有效的方法?
答案 0 :(得分:2)
在谈论效率时,必须在代码维护和性能之间有所不同。在Lua中,就像在大多数语言中一样,这两点分开了。
总是很容易使用pairs
代替ipairs
,通过table.insert
将元素附加到表格,通过..
连接字符串等等。但是这不是快速运行程序的方式。
每个Lua程序员应该阅读的一个文档:Lua Performance Tips by Roberto Ierusalimschy
致你的代码:
table.insert
,管理表格尺寸和尺寸。你自己插入。ipairs
。newPlayer
)。_
作为未使用变量名称的占位符(k
)。对于LuaJIT,还有一些其他规则,因为编译器部分进行了大量优化,例如: (i)pairs
减速的速度要小得多。