Reputation: 33
I wrote an Android application that communicates with a webserver(wampserver and php) using the class AsyncTask
here is its code:
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;
}
I used this class as an inner class in LoginActivity
which, upon pressing the button: authButton
, gets the email and password from the user, passes them to the server with GET method, verifies the existence of the user in the database, and if it exists it returns the username, else it returns "wrong login check again"
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);
}
}
});
}
When I run the app on my phone (samsung S4 Andoid 5.0.1 API 21) it appears to execute this statement:
LoginTask logintask = new LoginTask();
But, there is no trace of it and no result. Even the Log.d()
in the different methods of LoginTask
are not displayed in Logcat. Instead, this is what I get in Logcat when pressing the 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
this is the declaration code for publishProgress() :
@WorkerThread
protected final void publishProgress(Progress... values) {
if (!isCancelled()) {
getHandler().obtainMessage(MESSAGE_POST_PROGRESS,
new AsyncTaskResult<Progress>(this, values)).sendToTarget();
}
}
As for the Login.php, I tested it and it gives proper results, no problem there.
After adding printStackTrace()
, this is the message displayed in the log
java.io.FileNotFoundException: http://192.168.1.10/webapp/[email protected]&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)
Upvotes: 1
Views: 1254
Reputation: 814
Apparently the issue is that your wamp server is denying the connection, since that is the default behavior.
You can go to httpd.conf
config file. Then look for a section that starts with the Directory
tag. You can add Allow from all
for a quick test but that is a big security no-no.
So you need to do Allow from 192.168.0.0
, assuming 192.168.0.0
is your network address.
The specific syntax depends on your wamp server but this should get you in the right direction.
Upvotes: 1
Reputation: 94
Unsure what is exactly happening. However it may be helpful to you to treat the exception in that try-catch block. At the moment you're letting exceptions fall silently.
try {
}catch (Exception ex){}
return null;
}
Change that over to:
try {
....
}catch (Exception ex){
ex.printStackTrace();
}
return null;
}
Maybe you'll get some feedback with that. Just double checking you got the android.permission.INTERNET" permission in your manifest?
Also, try using network IP instead of localhost.
Upvotes: 0
Reputation: 97
Where is the variable?
NewsData = ConvertInputToStringNoChange(in);
Where is this function declaration?
publishProgress(NewsData);
You aren't returning anything in doBackground() function.
Upvotes: 0