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!
if ("1".equals(result)). Also,EntityUtilsas the methodtoStringthat takes as parameter anEntityand returns aString representing the entity itself. If I were in you I would rather use that thanconvertInputStreamToStringAcceptuseAccept-Encoding.