从服务器Android SDK运行PHP脚本
因此,在查找如何从android应用程序运行php脚本时,我遇到了以下代码:从服务器Android SDK运行PHP脚本,php,android,Php,Android,因此,在查找如何从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.c
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
}
最初,我认为这是可行的,因为client.execute执行url中的脚本。然而,直到现在我才意识到HttpGet从运行脚本中获取信息。我的PHP脚本不提供信息。它只在服务器上执行任务(如果我在Httpclient上出错,请更正我)
如何使client.execute只执行脚本,而不尝试从脚本中获取信息?我假设我的应用程序崩溃的原因是因为脚本没有返回任何值为null的信息
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。您是否尝试在broswer中执行您的php脚本?是的,它工作正常。确定您在php脚本中得到了什么响应?它所要做的就是创建一个文件夹并向其中添加一个文件。它使用$\u GET获取文件夹和文件的名称