【问题标题】:HttpPost request responseHttpPost 请求响应
【发布时间】:2023-03-25 04:58:01
【问题描述】:

我有一个网页以 1(真)或 0(假)响应记录请求。当我尝试用另一个网页调用请愿书时,结果是正确的,它可以是 0 或 1。但是当我用 Android 应用程序调用它时,结果总是为 0。

这是我的代码:

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", "test@test.com");

        } 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;

    }

谢谢!

【问题讨论】:

  • 你必须使用 equals 来比较字符串。像if ("1".equals(result)) 这样的东西。此外,EntityUtils 作为方法toString 接受Entity 作为参数并返回代表实体本身的String。如果我在你里面,我宁愿使用它而不是convertInputStreamToString
  • 正如@blackbelt 建议的那样,您也应该尝试一下。而不是Accept 使用Accept-Encoding

标签: android post http-post


【解决方案1】:

如果请求不返回 1 则预期数据不正确。错误可以是值或格式(不是 json)。

【讨论】:

    【解决方案2】:

    试试这样的:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多