Kfir Eichenblat
Kfir Eichenblat

Reputation: 469

Asynchronous Http Post in Android

I am working on a small test app that communicates with my website. It makes an HttpPost with credentials in order to see if the user is registered.

However, for some reason the post is made instantaniously even though it's supposed to take a second and it's always empty. It's a class I used about a year ago, was working back then.

private class LoginTask extends AsyncTask<String, Integer, String> {

    private HashMap<String, String> mData = null;// post data
    public String serverResult = "";
    /**
     * constructor
     */
    public LoginTask(HashMap<String, String> data) {
        mData = data;
    }

    /**
     * background
     */
    @Override
    protected String doInBackground(String... params) {
        byte[] result = null;
        String str = "";
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(params[0]);// in this case, params[0] is URL
        try {
            // set up post data
            ArrayList<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
            Iterator<String> it = mData.keySet().iterator();
            while (it.hasNext()) {
                String key = it.next();
                nameValuePair.add(new BasicNameValuePair(key, mData.get(key)));
            }

            post.setEntity(new UrlEncodedFormEntity(nameValuePair, "UTF-8"));
            HttpResponse response = client.execute(post);
            StatusLine statusLine = response.getStatusLine();
            if(statusLine.getStatusCode() == HttpURLConnection.HTTP_OK){
                result = EntityUtils.toByteArray(response.getEntity());
                str = new String(result, "UTF-8");
            }
        }
        catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        catch (Exception e) {
        }
        serverResult = str;
        return str;
    }

    /**
     * on getting result
     */
    @Override
    protected void onPostExecute(String result) {
        serverResult = result;
        pb.setVisibility(View.GONE);

        if(serverResult == "no")
        {
            Toast t = Toast.makeText(MainActivity.this, "Login failed. Correct username or password", Toast.LENGTH_LONG);
            t.show();
        }
        else if (serverResult.contains("email"))
        {
            Toast t = Toast.makeText(MainActivity.this, "HELLOOOOO", Toast.LENGTH_LONG);
            t.show();
        }
        else    
        {
            Toast t = Toast.makeText(MainActivity.this, "Server error: " + serverResult, Toast.LENGTH_LONG);
            t.show();
        }
    }

    protected void onProgressUpdate(Integer... progress) {
        pb.setProgress(progress[0]);
    }
}

The information sent to the server is definitely correct and should retrieve SOME message back, but here it's always empty. Maybe something in the background process is wrong.

Upvotes: 0

Views: 81

Answers (1)

Arnaud
Arnaud

Reputation: 509

What's going on in the logcat ? Do you have any exception there ?

And did you add the internet permission into your manifest ?

<uses-permission android:name="android.permission.INTERNET" />

Upvotes: 1

Related Questions