因此,在查找如何从android应用程序运行php脚本时,我遇到了以下代码:
final String url = "http://example.com/creatd.php?nm=mytest";
HttpClient client = new DefaultHttpClient();
try {
client.execute(new HttpGet(url));
Intent intent = new Intent(this, Main.class);
startActivity(intent);
} catch(IOException e) {
//do something here
}
最初,我认为这将工作,因为客户端。Execute执行url中的脚本。然而,我直到现在才意识到的是HttpGet从运行脚本中获取信息。我的PHP脚本不提供信息。它只是在服务器上执行一个任务(如果我在Httpclient上错了请纠正我)。
我该如何让客户。执行仅仅执行脚本,而不是试图从它获取信息?我假设我的应用程序崩溃的原因是,因为脚本不返回任何信息的值为空。
PHP代码:<?php
$str = $_GET['nm'];
mkdir($str,0700);
fopen($str."/".$str.".meb", "w");
file_put_contents($str."/".$str.".meb", "Hello, world!", FILE_APPEND | LOCK_EX);
?>
编辑:只是一个问题,为什么人们总是不给我的问题投票?我研究了我的问题,并尝试使用解决方案,但提供的解决方案不起作用。我做错了什么? 多亏了Ghost,我找到了解决问题的方法,并将其添加到线程中。
final String url = "http://example.com/perform.php?nm="+string;
new Thread() {
@Override
public void run() {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet(url));
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
response.getEntity().writeTo(out);
out.close();
} catch (IOException e) {
}
String responseString = out.toString();
//..more logic
} else {
//Closes the connection.
try {
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
} catch (IOException e) {
}
}
}
catch (ClientProtocolException e)
{
}
catch (IOException e)
{
}
}
}.start();
再次感谢Ghost。