0

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.

2
  • Add a log statement before str = new String(result, "UTF-8"); and see the value of result. Add another log statement after the catch blocks for str. See what happens Commented Dec 18, 2013 at 19:20
  • Have you stepped through this? You could be getting some other exception which you're not seeing in Logcat because you're eating exceptions: catch (Exception e) { } Commented Dec 18, 2013 at 19:28

1 Answer 1

1

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" /> 
Sign up to request clarification or add additional context in comments.

1 Comment

There was no exception, the permission was the problem. Having no permission must have caused the device to skip the post part, so there was an instantanious empty result.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.