I am trying to send a POST request to a php file which then sends back a json message for username and password authentication. with my code I get an error ArrayIndexOutOfBoundsException: length=0; index=0 on this line:
HttpGet get=new HttpGet(strings[0]);
In my code, when the login button is clicked, it calls an asyncTask class which then runs the application. it crashes before I can even test the code but I am not sure why. I am new to HttpClient so please be gentle. This is my snippet of code in the asynctask class
public class DownloadFilesTask extends AsyncTask<Void, Void, Void> {
private String name, pwd;
private LoginActivity loginActivity;
boolean bloggedIn;
public DownloadFilesTask(LoginActivity loginActivity,String name, String pwd){
this.loginActivity=loginActivity;
this.name=name;
this.pwd=pwd;
}
@Override
protected Void doInBackground(Void... voids) {
HttpClient httpClient=new DefaultHttpClient();
HttpGet get=new HttpGet();
try {
URI website=new URI("login.php");
get.setURI(website);
} catch (URISyntaxException e) {
e.printStackTrace();
}
HttpPost httpPost=new HttpPost("login.php");
List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>(2);
String result=null;
nameValuePairs.add(new BasicNameValuePair("name", name));
nameValuePairs.add(new BasicNameValuePair("password", pwd));
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
HttpResponse response = null; //error
try {
response = httpClient.execute(httpPost);
} catch (IOException e) {
e.printStackTrace();
}
HttpEntity entity=response.getEntity();
InputStream instream= null;
try {
instream = entity.getContent();
} catch (IOException e) {
e.printStackTrace();
}
result=convertStreamToString(instream);
try {
instream.close();
} catch (IOException e) {
e.printStackTrace();
}
if (Utility.isNotNull(name) && Utility.isNotNull(pwd)) {
RequestParams params = new RequestParams();
if (Utility.validate(name, pwd)) {
params.put("username", name);
params.put("password", pwd);
bloggedIn=true;
onPostExecute();
} else {
loginActivity.InvalidToast();
}
} else {
loginActivity.EmptyToast();
}
return null;
}
private String convertStreamToString(InputStream instream) {
BufferedReader reader=new BufferedReader(new InputStreamReader(instream));
StringBuilder sb=new StringBuilder();
String line=null;
try {
while ((line=reader.readLine())!=null){
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
instream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
protected void onPostExecute(){
if (bloggedIn=true) {
loginActivity.navigatetoMainActivity();
}else{
loginActivity.InvalidToast();
}
}
private static class Utility {
static Pattern pattern;
static Matcher matcher;
static Pattern pattern1;
static Matcher matcher1;
static String NAME_PATTERN="ABCDEF";
static String PWD_PATTERN="ghijkl";
public static boolean validate(String name,String pwd){
pattern=Pattern.compile(NAME_PATTERN);
pattern1=Pattern.compile(PWD_PATTERN);
matcher=pattern.matcher(name);
matcher1=pattern1.matcher(pwd);
return matcher.matches()&& matcher1.matches();
}
public static boolean isNotNull(String name) {
return name!=null && name.trim().length()>0 ? true: false;
}
}
}