我正在制作一个应用程序,并在该应用程序中,我需要发送数据到php服务器和接收数据在php服务器,反之亦然。我无法将数据从android应用程序发送到php服务器?你能告诉我该怎么做吗?
这是我发送数据到服务器的代码。
如何发送数据到php服务器和使用POST和httpurl连接在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类。
在此先感谢。
等待你的答复。提前致谢。 – Sateesh 2014-10-20 09:26:13