Java android应用程序URL连接获取请求无法工作
我有个问题 我不明白为什么下面的代码不能工作 这是一个简单的应用程序,它向一些php片段发送GET请求,这些php片段应该在表中插入一行 我不明白为什么它不起作用 请帮忙 爪哇: php:Java android应用程序URL连接获取请求无法工作,java,php,android,mysql,get,Java,Php,Android,Mysql,Get,我有个问题 我不明白为什么下面的代码不能工作 这是一个简单的应用程序,它向一些php片段发送GET请求,这些php片段应该在表中插入一行 我不明白为什么它不起作用 请帮忙 爪哇: php: 您需要使网络操作脱离主线程 我刚刚使用AsyncTask实现并测试了此功能: class MyAsyncTask extends AsyncTask<Void, Void, String> { @Override protected String doInBackground(V
您需要使网络操作脱离主线程
我刚刚使用AsyncTask实现并测试了此功能:
class MyAsyncTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
StringBuilder result = new StringBuilder();
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL("http://www.chatty.co.il/biyum.php?q=mkyong");
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader isw = new InputStreamReader(in);
int data = isw.read();
while (data != -1) {
char current = (char) data;
data = isw.read();
result.append(current);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
return result.toString();
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(getApplicationContext(),"GET:"+result,Toast.LENGTH_LONG).show();
}
}
致祝酒辞:
<?php
require('connect.inc.php');
$update_offline="INSERT INTO offline_waiters (user_partner,mail,chat_room) VALUES(0,'gavno','gavno')";
$query_run= mysqli_query($conn,$update_offline);
echo "new";
?>
class MyAsyncTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
StringBuilder result = new StringBuilder();
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL("http://www.chatty.co.il/biyum.php?q=mkyong");
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader isw = new InputStreamReader(in);
int data = isw.read();
while (data != -1) {
char current = (char) data;
data = isw.read();
result.append(current);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
return result.toString();
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(getApplicationContext(),"GET:"+result,Toast.LENGTH_LONG).show();
}
}
startBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
new MyAsyncTask().execute();
});