我已经看过其他几个问题,但我仍然不完全理解这一点.我想将JSON字符串POST到远程地址,然后从JSON响应中检索值.我正在使用Apache的Java库.
public HttpResponse http(String url, String body) throws IOException {
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
HttpPost request = new HttpPost(url);
StringEntity params = new StringEntity(body);
request.addHeader("content-type", "application/json");
request.setEntity(params);
//httpClient.execute(request);
HttpResponse result = httpClient.execute(request);
} catch (IOException ex) {
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
作为正文,我将通过以下(示例):
{"example":1,"fr":"lol"}
Run Code Online (Sandbox Code Playgroud)
我完全不知道如何从响应中检索JSON值.
那么我们为什么要缓存jQuery对象呢?
在以下场景中:
var foo = $('#bar');
foo.attr('style','cool');
foo.attr('width','123');
Run Code Online (Sandbox Code Playgroud)
$('#bar').attr('style','cool');
$('#bar').attr('width','123');
为什么第一个选项比第二个选项好得多?
如果是因为性能,它如何减少使用?
所以我正在创建一个横幅生成器.
我将在中间添加文本,但希望它完全位于中心.我知道imagettftext
可以用来写入横幅,但这不会使它居中.
一个可能的解决方案可能是找到文本的宽度,然后使用一半从横幅宽度的一半取出,但我不知道如何做到这一点.
我使用的是PHP-GD,不想使用其他任何我必须安装的东西.
imagettftext($img, 14, 0, (468 - ((strlen($_GET['description']) * imagefontwidth(imageloadfont('minecraft.ttf'))) / 1)), 85, imagecolorallocate($img, 0, 0, 0), 'minecraft.ttf', $_GET['description']);
Run Code Online (Sandbox Code Playgroud)
上面的代码是上面的结果.小字符串很好,但一定有问题,因为一旦它们变长,它就会失败.
到目前为止,我有以下代码。它在 HTML5 画布上绘制一行等距图块。但是,我想创建一个完整的楼层,而不仅仅是一行,尽管我尝试了很多,但还是失败了。
function createEmptyMapRow(x, r /* The offset in tiles. Setting this to 1 draws one row down. */)
{
var cx = 0, lx = 0, ly = 0;
while(cx != x)
{
renderImage(lx - (r * 32), ly + (r * 14), 'tile.png');
lx = lx + 32;
ly = ly + 14;
cx++;
}
}
Run Code Online (Sandbox Code Playgroud)
如果你不想写代码,就给我一些逻辑。
我希望能够创建一个函数,为每一行从左到右放置图块。
问题是要求比较成本for
和while
循环,相对于if
陈述的成本.
假设if语句的相对成本为1,for和while循环的相对成本是多少?
假设在比较时将单个布尔值传递给while
循环和if
语句,并while
允许迭代X次.假设它for
也迭代X次.
java ×2
javascript ×2
performance ×2
caching ×1
canvas ×1
for-loop ×1
gd ×1
html ×1
html5-canvas ×1
http ×1
if-statement ×1
jquery ×1
json ×1
php ×1
php-gd ×1
while-loop ×1