Hello guys am new to android and currently learning android. Am making login system for my app. Here is my asynctask
@Override
protected String doInBackground(String... params) {
BufferedReader in = null;
ArrayList<NameValuePair> dataToSend = new ArrayList<>();
dataToSend.add(new BasicNameValuePair("user_email", user.email));
dataToSend.add(new BasicNameValuePair("user_pass", user.password));
HttpParams httpRequestParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpClient client = new DefaultHttpClient(httpRequestParams);
HttpPost post = new HttpPost(SERVER_ADDRESS + "login.php");
User returnedUser= null;
try {
post.setEntity(new UrlEncodedFormEntity(dataToSend));
HttpResponse httpResponse = client.execute(post);
HttpEntity entity = httpResponse.getEntity();
in = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
String response = "";
String line = "";
while ((line = in.readLine())!= null){
response+= line;
}
in.close();
Log.d("qwerty", response);
if(response.equals("notAct\t\t")){
return "notAct";
}else if(response.equals("Error\t\t")){
return "Error";
}
String result = EntityUtils.toString(entity);
final JSONObject jObject = new JSONObject(result);
if (jObject.length() == 0) {
progressDialog.dismiss();
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Connection Error json");
builder.setPositiveButton("ok", null);
builder.show();
} else {
String user_id = jObject.getString("user_id");
String user_name = jObject.getString("user_name");
returnedUser = new User(user.email, user.password, user_name, user_id);
Log.d("qwerty", "Exception time");
userCallback.done(returnedUser);
}
}catch(NullPointerException e){
e.printStackTrace();
} catch (JSONException e){
e.printStackTrace();
} catch(ArithmeticException e) {
e.printStackTrace();
} catch (ConnectTimeoutException e) {
Log.d("qwerty", "RunTime");
return "Abc";
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
try {
if (result.equals("notAct")) {
//account activated
} else if (result.equals("Error")) {
//email does't exist
}else if (result.equals("Abc")) {
//connection time out
}
}catch(NullPointerException e){
//null pointer exception
}catch (RuntimeException e){
//
} catch (Exception e){
//
}
super.onPostExecute(result);
}
All exceptions are handeled but json and String which are taken from a php file by this asynctask is not working together
if I write the coding of Json before Reading String (String Response) then json Read by it but it is unable to read the error like if user entered the incorrect user details. Thanks in advance for Helping.