1

I have a web page that responds to a request for logging with 1 (true) or 0 (false). When I try to call the petition with another web page the result is correct and it can be 0 or 1. But when I call it whit an Android App the result always is 0.

This is my code:

protected String doInBackground(String... params) { InputStream inputStream = null; String result = ""; String sJSon = ""; HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost("http://myURL"); //Build jsonObject JSONObject jsonObject = new JSONObject(); try { jsonObject.accumulate("token", "123456789"); } catch (JSONException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { jsonObject.accumulate("passw", "test"); } catch (JSONException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { jsonObject.accumulate("email", "[email protected]"); } catch (JSONException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } // Convert JSONObject to JSON to String sJSon = jsonObject.toString(); //Encoding POST data try { httpPost.setEntity(new StringEntity(sJSon)); //Set some headers to inform server about the type of the content httpPost.setHeader("Accept", "application/json"); httpPost.setHeader("Content-type", "application/json"); } catch (UnsupportedEncodingException e) { // log exception e.printStackTrace(); } //making POST request. try { HttpResponse response = httpClient.execute(httpPost); // write response to log // receive response as inputStream inputStream = response.getEntity().getContent(); // convert inputstream to string if(inputStream != null) result = convertInputStreamToString(inputStream); //THE RESULT SHOULD BE 1 AND NO 0 WHEN THE LOGGING IS OK. BUT IT IS ALWAYS 0 else result = "Did not work!"; } catch (ClientProtocolException e) { // Log exception e.printStackTrace(); } catch (IOException e) { // Log exception e.printStackTrace(); } if (result == "1"){ Intent intent = new Intent(LoginActivity.this, MenuActivity.class); startActivity(intent); } return null; } private static String convertInputStreamToString(InputStream inputStream) throws IOException{ BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String line = ""; String result = ""; while((line = bufferedReader.readLine()) != null) result += line; inputStream.close(); return result; } 

Thank you!

2
  • 3
    you have to use equals to compare strings. Something like if ("1".equals(result)). Also, EntityUtils as the method toString that takes as parameter an Entity and returns a String representing the entity itself. If I were in you I would rather use that than convertInputStreamToString Commented Oct 7, 2014 at 7:21
  • 1
    As suggested @blackbelt you should also try this. Instead of Accept use Accept-Encoding. Commented Oct 7, 2014 at 7:26

2 Answers 2

1

If the request does not return 1 is expected that the data are not correct . The error can be by value or by its format ( not to be json ).

Sign up to request clarification or add additional context in comments.

Comments

1

Try something like this:

HttpResponse response = httpclient.execute(method); HttpEntity entity = response.getEntity(); if(entity != null){ return EntityUtils.toString(entity); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.