-2

enter image description hereI have been trying for hours to send a POST request to a php which sends back a json response. But that one line of code has been giving me problems and I cannot seem to move forward. I am so frustrated. I have looked up countless examples of others using the same line! but for some reason it won't work for me! this is my code snippet for my doinBackgrounds:

protected Void doInBackground(Void... voids) {
    HttpClient httpClient=new DefaultHttpClient();
    HttpGet request=new HttpGet();
    BufferedReader in=null;
    String data=null;
    HttpResponse response = null;
    try {
        URI website=new URI("login.php");
        request.setURI(website);
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }

    HttpPost httpPost=new HttpPost("login.php");
    List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("name", name));
    nameValuePairs.add(new BasicNameValuePair("password", pwd));
    try {
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    try {
        response=httpClient.execute(request);
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        in=new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        String line=in.readLine();
        System.out.println(line);
    } 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;
}
Aria
  • 389
  • 3
  • 7
  • 25

1 Answers1

0

You are calling doInBackground() yourself. This is not how you use AsyncTask. To execute an AsyncTask, create an instance and call execute() or executeOnExecutor() on it. That will cause doInBackground() to be run on a background thread.

You can read more about AsyncTask in the JavaDocs.

(BTW, in the future, please post stack traces as text copied from LogCat, not screenshots)

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491