AsyncTask 不返回任何响应
Posted
技术标签:
【中文标题】AsyncTask 不返回任何响应【英文标题】:AsyncTask not returning any response 【发布时间】:2017-12-08 18:27:17 【问题描述】:您好,我无法从主机获取我的网址的响应。我尝试放置静态变量并且它有效,但是当我尝试使用 asynctask 进行响应时它不起作用。
<?php
include_once('connection.php');
$where='';
if(isset($_GET['userLat']) && isset($_GET['userLng']))
$where = "WHERE ((userLat >= '".addslashes($_GET['userLat'])."' AND userLat <= '".addslashes($_GET['userLat'])."' + .00901) OR (userLat <= '".addslashes($_GET['userLat'])."' AND userLat >= '".addslashes($_GET['userLat'])."' - .00901)) AND ((userLng >= '".addslashes($_GET['userLng'])."' AND userLng <= '".addslashes($_GET['userLng'])."' + .014935) OR (userLng <= '".addslashes($_GET['userLng'])."' AND userLng >= '".addslashes($_GET['userLng'])."' - .014935))";
$query = "SELECT * FROM tbl_user ".$where." AND isOnline = 'Yes' LIMIT 1 ";
$result = mysqli_query($conn, $query);
$json = array();
if(mysqli_num_rows($result))
while($row = mysqli_fetch_assoc($result))
$json['details'][]=$row;
mysqli_close($conn);
echo json_encode($json);
?>
异步任务
private class JsonTask extends AsyncTask<String, String, String>
protected void onPreExecute()
super.onPreExecute();
pd = new ProgressDialog(getActivity());
pd.setMessage("Checking nearby wingmans");
pd.setCancelable(false);
pd.show();
protected String doInBackground(String... params)
HttpURLConnection connection = null;
BufferedReader reader = null;
try
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null)
buffer.append(line + "\n");
Log.d("Response: ", "> " + line); //here u ll get whole response...... :-)
return buffer.toString();
catch (MalformedURLException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
finally
if (connection != null)
connection.disconnect();
try
if (reader != null)
reader.close();
catch (IOException e)
e.printStackTrace();
return null;
@Override
protected void onPostExecute(String result)
super.onPostExecute(result);
if (pd.isShowing())
pd.dismiss();
request = result;
Log.d("Test", result);
我如何称呼我的网址
btnEmergency.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
String url = "http://10.0.2.2/wingman/emergency.php?userLat="+tvLat.getText().toString()+"ANDuserLng="+tvLng.getText().toString();
new JsonTask().execute(url);
);
谢谢大家,我希望我能得到我的问题的答案。谢谢:)
【问题讨论】:
是否显示异常日志? 你真的应该发布日志,这可能有很多原因,首先想到的是互联网许可 @UsmanRana - 也没有异常日志 【参考方案1】:我太傻了。我将我的网址中的AND
更改为&
来自
String url = "http://10.0.2.2/wingman/emergency.php?userLat="+tvLat.getText().toString()+"ANDuserLng="+tvLng.getText().toString();
到
String url = "http://10.0.2.2/wingman/emergency.php?userLat="+tvLat.getText().toString()+"&userLng="+tvLng.getText().toString();
它奏效了。谢谢!
【讨论】:
以上是关于AsyncTask 不返回任何响应的主要内容,如果未能解决你的问题,请参考以下文章