Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android HttpPost请求响应_Android_Post_Http Post - Fatal编程技术网

Android HttpPost请求响应

Android HttpPost请求响应,android,post,http-post,Android,Post,Http Post,我有一个网页,它用1(true)或0(false)响应日志记录请求。当我尝试用另一个网页调用请愿书时,结果是正确的,可以是0或1。但当我用Android应用程序来称呼它时,结果总是0 这是我的代码: protected String doInBackground(String... params) { InputStream inputStream = null; String result = ""; String sJSon = "";

我有一个网页,它用1(true)或0(false)响应日志记录请求。当我尝试用另一个网页调用请愿书时,结果是正确的,可以是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;

    }

谢谢大家!

试试这样的方法:

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

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

您必须使用equals来比较字符串。类似于
if(“1”.equals(result))
。另外,
EntityUtils
作为方法
toString
,该方法将
实体
作为参数,并返回表示实体本身的
Strin
g。如果我是你的话,我宁愿使用它,而不是像建议的那样@blackbelt使用
convertInputStreamToString
,你也应该试试这个。不要使用
Accept
而使用
Accept Encoding