【发布时间】:2014-10-20 09:03:06
【问题描述】:
我正在制作一个应用程序,在该应用程序中我需要将数据发送到 php 服务器并在 php 服务器接收数据,反之亦然。我无法将数据从 android 应用程序发送到 php 服务器?你能告诉我怎么做吗?
这是我向服务器发送数据的代码。
public class EventServiceHandler
{
String data;
DoSomething d = new DoSomething();
public EventServiceHandler() {
// TODO Auto-generated constructor stub
d.execute();
}
public void getObject()
{
String s = "http://www.example.com/thisisthis.php";
try {
URL url = new URL(s);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setConnectTimeout(100000);
connection.setReadTimeout(100000);
connection.connect();
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
response=connection.getResponseCode();
String name="sateesh";
String testing=URLEncoder.encode("name", "utf-8")
+ "=" + URLEncoder.encode(name, "utf-8");
out.write(testing);
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private class DoSomething extends AsyncTask<Void, Void, Void>
{
@Override
protected Void doInBackground(Void... params)
{
// TODO Auto-generated method stub
getObject();
return null;
}
}
}
这是 php 服务器上的代码,用于在服务器上接收数据。
<?php
if($_POST)
{
$name = urldecode($_POST['name']);
echo $name;
error_log($name,0);
echo " ok";
}
else if(is_null($_POST) OR empty($_POST))
{
error_log("empty or null",0);
echo "in else if";
}
else
{
echo "<br>";
echo "in else";
}
echo "just";
?>
我如何知道我正在接收数据,如何确保正在接收数据。
我是 php 新手,从未在 android 中使用过 http 类。
提前致谢。
【问题讨论】:
-
等待您的回复。提前致谢。
标签: php android httpurlconnection