Java android HTTP请求失败
我编写了一个Android应用程序,它使用类Java android HTTP请求失败,java,android,web-services,http,Java,Android,Web Services,Http,我编写了一个Android应用程序,它使用类AsyncTask与Web服务器(wampserver和php)进行通信。下面是它的代码: public class LoginTask extends AsyncTask<String, String, String> { @Override protected void onPreExecute() { Log.d("", "onPreExecute: "); } @Override
AsyncTask
与Web服务器(wampserver和php)进行通信。下面是它的代码:
public class LoginTask extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
Log.d("", "onPreExecute: ");
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
Log.d("", "doInBackground: ");
try {
String NewsData;
//define the url we have to connect with
URL url = new URL(params[0]);
//make connect with url and send request
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//waiting for 7000ms for response
urlConnection.setConnectTimeout(7000);//set timeout to 5 seconds
try {
//getting the response data
/*Line : 117*/InputStream in = new BufferedInputStream(urlConnection.getInputStream());
//convert the stream to string
NewsData = ConvertInputToStringNoChange(in);
//send to display data
publishProgress(NewsData);
} finally {
//end connection
urlConnection.disconnect();
}
}catch (Exception ex){}
return null;
}
protected void onProgressUpdate(String... progress) {
try {
Toast.makeText(getApplicationContext(),progress[0],Toast.LENGTH_LONG).show();
Log.d("PROGRESS ??", progress[0]);
//response = progress[0];
alertDialog.setMessage(progress[0]);
alertDialog.show();
response = true;
} catch (Exception ex) {
}
}
protected void onPostExecute(String result2){
response = true;
}
}
public static String ConvertInputToStringNoChange(InputStream inputStream) {
BufferedReader bureader=new BufferedReader( new InputStreamReader(inputStream));
String line ;
String linereultcal="";
try{
while((line=bureader.readLine())!=null) {
linereultcal+=line;
}
inputStream.close();
}catch (Exception ex){}
return linereultcal;
}
当我在手机上运行应用程序(三星S4 Andoid 5.0.1 API 21)时,它似乎执行以下语句:
LoginTask logintask = new LoginTask();
但是,没有它的踪迹,也没有结果。即使是LoginTask
的不同方法中的Log.d()
,也不会显示在Logcat中。相反,当按下auth按钮时,这是我在Logcat中得到的:
(HTTPLog)-Static: isSBSettingEnabled false
(HTTPLog)-Static: isShipBuild true
(HTTPLog)-Thread-14638-1006967439: SmartBonding Enabling is false, SHIP_BUILD is true, log to file is false, DBG is false
(HTTPLog)-Static: isSBSettingEnabled false
KnoxVpnUidStorageknoxVpnSupported API value returned is false
这是publishProgress()的声明代码:
变量在哪里
NewsData = ConvertInputToStringNoChange(in);
这个函数声明在哪里
publishProgress(NewsData);
在doBackground()函数中没有返回任何内容。不确定到底发生了什么。
但是,在try-catch块中处理异常可能会有所帮助。此时此刻,你正让例外悄然降临
try {
}catch (Exception ex){}
return null;
}
换成:
try {
....
}catch (Exception ex){
ex.printStackTrace();
}
return null;
}
也许你会得到一些反馈。
只要仔细检查一下你的清单上是否有“安卓.权限.互联网”权限
另外,尝试使用网络IP而不是本地主机。显然,问题在于wamp服务器拒绝连接,因为这是默认行为
您可以转到httpd.conf
config文件。然后查找以Directory
标记开头的部分。您可以添加Allow
进行快速测试,但这是一个很大的安全问题
因此,假设192.168.0.0
是您的网络地址,您需要执行Allow from 192.168.0.0
具体语法取决于您的wamp服务器,但这应该会让您找到正确的方向。ConvertInputToStringNoChange()是在LoginTask代码末尾声明的,请检查第一个代码段的最后几行。publishProgress()是在AsyncTask中声明的预定义方法。它应该返回doInBackround()的结果到onProgressUpdate()。我在帖子中添加了声明代码。您不需要使用此方法。在进程中使用onProgressUpdate,在进程完成时使用OnPostUpdate。避免将线程对象用于AsyncTask对象。其他事项:新闻数据的内容应在doInBackground中返回,并在OnPostUpdate中恢复,您可以在其中更新屏幕。我在doInBackround中添加NewsData并在onPostExecute中添加处理:受保护的void onPostExecute(字符串结果){if(result!=null&!result.equals(“再次错误登录检查”){Log.d(“onPostExecute结果:,“良好结果”);Intent Intent=new Intent(LoginActivity.this,MainActivity.class);intent.putExtra(“名称”,结果);intent.putExtra(“邮件”,电子邮件);startActivity(intent);}else{Log.d(“onPostExecute结果:”,结果);}}和“结果”"总是为空。我如你所说。还有一个例外:192.168.1.10是你局域网中服务器的IP吗?它是pc ipv4 IP地址:命令提示符-->ipconfig-->ipv4地址你需要提供服务器的IP,而不是localhost。我在WampServer中使用的是localhost服务器。如何获取服务器IP?在你的一条评论中,你需要n使用服务器IP。只是为了确保您可以通过移动浏览器访问您的wamp服务器?两者应该在同一网络上,wamp必须允许从外部连接。我知道,我尝试使用服务器IP从手机导航器访问服务器,但似乎不允许,它主要显示:禁止您没有访问权限s/在此服务器上。(PC和手机都连接到同一网络)这一定是我的问题,我如何解决它?
publishProgress(NewsData);
try {
}catch (Exception ex){}
return null;
}
try {
....
}catch (Exception ex){
ex.printStackTrace();
}
return null;
}