我写了一个Android应用程序,它使用类AsyncTask
与网络服务器(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;
}
我在LoginActivity
中使用此类作为内部类,按下按钮:authButton
,从用户获取电子邮件和密码,使用GET方法将它们传递给服务器,验证数据库中存在用户,如果存在,则返回用户名,否则返回错误的登录检查&#34;
public class LoginActivity extends AppCompatActivity{
private AutoCompleteTextView mEmailView;
private TextInputEditText mPasswordView;
private Button authBtn;
private String email;
private String password;
private boolean response = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mEmailView = (AutoCompleteTextView) findViewById(R.id.email);
mPasswordView = (TextInputEditText) findViewById(R.id.password);
authBtn = (Button) findViewById(R.id.email_sign_in_button);
authBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
email = mEmailView.getText().toString().trim();
password = mPasswordView.getText().toString().trim();
if (email.isEmpty() || password.isEmpty()) {
//blah blah blah
} else {
String urlString = "http://localhost/webapp/login.php?email="+email+"&user_pass="+password;
LoginTask logintask = new LoginTask();
logintask.execute(urlString);
}
}
});
}
当我在手机上运行应用程序(三星S4 Andoid 5.0.1 API 21)时,似乎执行此声明:
LoginTask logintask = new LoginTask();
但是,没有任何痕迹,也没有结果。即使Log.d()
的不同方法中的LoginTask
也未显示在Logcat中。相反,这是我在按下authButton
:
(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()的声明代码:
@WorkerThread
protected final void publishProgress(Progress... values) {
if (!isCancelled()) {
getHandler().obtainMessage(MESSAGE_POST_PROGRESS,
new AsyncTaskResult<Progress>(this, values)).sendToTarget();
}
}
对于Login.php,我测试了它,它给出了正确的结果,没有问题。
添加printStackTrace()
后,这是日志中显示的消息
java.io.FileNotFoundException: http://192.168.1.10/webapp/login.php?email=example.example@gmail.com&user_pass=azerty
W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:206)
W/System.err: at com.ghazelatc.karim.monboutique.LoginActivity$LoginTask.doInBackground(LoginActivity.java:117)
W/System.err: at com.ghazelatc.karim.monboutique.LoginActivity$LoginTask.doInBackground(LoginActivity.java:94)
W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:288)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W/System.err: at java.lang.Thread.run(Thread.java:818)
答案 0 :(得分:1)
显然问题是您的wamp服务器拒绝连接,因为这是默认行为。
您可以转到httpd.conf
配置文件。然后查找以Directory
标记开头的部分。您可以添加Allow from all
进行快速测试,但这是一个很大的安全禁忌。
因此,您需要执行Allow from 192.168.0.0
,假设192.168.0.0
是您的网络地址。
具体的语法取决于你的wamp服务器,但这应该让你朝着正确的方向。
答案 1 :(得分:0)
变量在哪里?
NewsData = ConvertInputToStringNoChange(in);
这个函数声明在哪里?
publishProgress(NewsData);
你没有在doBackground()函数中返回任何内容。
答案 2 :(得分:0)
不确定究竟发生了什么。 但是,在try-catch块中处理异常可能会有所帮助。目前,你让异常无声无息。
try {
}catch (Exception ex){}
return null;
}
将其改为:
try {
....
}catch (Exception ex){
ex.printStackTrace();
}
return null;
}
也许你会得到一些反馈。 只需仔细检查你的清单中是否有android.permission.INTERNET“权限?
此外,尝试使用网络IP而不是localhost。