HttpPost请求响应

时间:2014-10-07 07:19:37

标签: android post http-post

我有一个网页响应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;

    }

谢谢!

2 个答案:

答案 0 :(得分:1)

尝试这样的事情:

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

答案 1 :(得分:1)

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