Java 如何将数据发送到php服务器并将数据接收到android
我是android编程的新手,我想将数据发送到php服务器并接收数据以在android中显示 但我不知道如何创建它,我不知道如何使用库?可以给我举一些例子来练习 对不起,我的英语不好 示例Myphp“xxx.xxx.x.x/api.php”Java 如何将数据发送到php服务器并将数据接收到android,java,android,Java,Android,我是android编程的新手,我想将数据发送到php服务器并接收数据以在android中显示 但我不知道如何创建它,我不知道如何使用库?可以给我举一些例子来练习 对不起,我的英语不好 示例Myphp“xxx.xxx.x.x/api.php” 我想从edittext向php服务器发送关键字和索引,并接收json数据以显示listview。HttpURLConnection可用于获取、放置、发布和删除请求: try { // Construct the URL
我想从edittext向php服务器发送
关键字
和索引
,并接收json数据以显示listview。HttpURLConnection可用于获取、放置、发布和删除请求:
try {
// Construct the URL
URL url = new URL("http://xxxxxxx/webservice/&query="
+keyword.getText().toString()
+"&index="+index.getText().toString());
// Create the request
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
// Read the input stream into a String
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
// Nothing to do.
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
// Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
// But it does make debugging a *lot* easier if you print out the completed
// buffer for debugging.
buffer.append(line + "\n");
}
if (buffer.length() == 0) {
// Stream was empty. No point in parsing.
}
JsonStr = buffer.toString(); //result
} catch (IOException e) {
}
其中
关键字
和索引
将是您的编辑文本变量。哦!对不起,我忘了告诉php文件名“111.11.11/api.php”
,无法使用http://xxxxxxx/webservice/&query=“…
ok然后将url替换为111.11.11/api.php?&query=“+关键字.getText().toString()+”&index=“+index.getText().toString()))我收到错误行返回null
无法从具有无效结果类型的方法返回值。connect()
是多余的。输入流不能为null,因此对此进行测试毫无意义。异常应该传播到调用方,而不是被吞没。@EJP您能给我举个例子吗?或者告诉我该怎么办
try {
// Construct the URL
URL url = new URL("http://xxxxxxx/webservice/&query="
+keyword.getText().toString()
+"&index="+index.getText().toString());
// Create the request
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
// Read the input stream into a String
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
// Nothing to do.
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
// Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
// But it does make debugging a *lot* easier if you print out the completed
// buffer for debugging.
buffer.append(line + "\n");
}
if (buffer.length() == 0) {
// Stream was empty. No point in parsing.
}
JsonStr = buffer.toString(); //result
} catch (IOException e) {
}