2014-10-07 249 views
1

我有一個網頁,響應與1(真)或0(假)記錄的請求。當我嘗試調用信訪與其他網頁的結果是正確的,它可以是0或1。但是,當我把它稱爲白衣的Android應用程序的結果總是爲0HttpPost請求響應

這是我的代碼:

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; 

    } 

謝謝!

+3

你必須使用equals來比較字符串。像「if(」1「.equals(result))''。另外,'EntityUtils'作爲'toString'方法,將'Entity'作爲參數,並返回表示實體本身的'Strin'g。如果我在你身上,我寧願使用它,而不是'convertInputStreamToString' – Blackbelt 2014-10-07 07:21:46

+1

建議@blackbelt你也應該試試這個。而不是'Accept'使用'Accept-Encoding'。 – User12111111 2014-10-07 07:26:10

回答

1

如果不返回1請求預計該數據是不正確的。錯誤可能是按值或按其格式(不是json)。

1

嘗試這樣:

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