Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.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 如何在HttpUrlConnection中使用POST方法_Android - Fatal编程技术网

Android 如何在HttpUrlConnection中使用POST方法

Android 如何在HttpUrlConnection中使用POST方法,android,Android,我试图在doInBackground方法内的异步任务中使用HttpUrlConnection类 @Override protected String doInBackground(String... params) { String strUrl=String.format("%s",SERVER_URL); try { URL url =new URL(strUrl); HttpURLConnection

我试图在doInBackground方法内的异步任务中使用HttpUrlConnection类

@Override
    protected String doInBackground(String... params) {
        String strUrl=String.format("%s",SERVER_URL);
        try {

            URL url =new URL(strUrl);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("POST");
            urlConnection.setDoOutput(true);
            urlConnection.setDoInput(true);
            urlConnection.setChunkedStreamingMode(0);
            urlConnection.setRequestProperty("Content-Type","application/json");

            String str="{\"userid\":\"1\"}";
            byte[] outputInBytes = str.getBytes("UTF-8");
            OutputStream os = urlConnection.getOutputStream();
            os.write( outputInBytes );
os.close();


            InputStream stream1=urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(stream1);
        StringBuilder builder = new StringBuilder();
        int chr;
        while((chr=reader.read())!=-1)
        {
            builder.append((char)chr);
        }
        String result=builder.toString();

 return result;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
 return null;
    }

但是我得到的是null而不是result,这里的问题是什么。

最后,我的代码运行良好,它需要一些更改,如:-

通过这种方法,我们的URL被分块成特定的数据包

urlConnection.setChunkedStreamingMode(0);//No use in my code
由于我使用的是硬编码的JSON字符串,它被视为一个简单的字符串。因此,我必须使用下面给出的“application/x-www-form-urlencoded”,而不是“application/JSON”

urlConnection.setRequestProperty("Content-Type","application/json");//incorrect
urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");//when we need to pass string inside POST body.
它用于连接到POST方法URL

urlConnection.connect();

如果我在某个地方有错误的概念,请让我知道。

最后,我的代码运行良好,它需要一些更改,如:-

通过这种方法,我们的URL被分块成特定的数据包

urlConnection.setChunkedStreamingMode(0);//No use in my code
由于我使用的是硬编码的JSON字符串,它被视为一个简单的字符串。因此,我必须使用下面给出的“application/x-www-form-urlencoded”,而不是“application/JSON”

urlConnection.setRequestProperty("Content-Type","application/json");//incorrect
urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");//when we need to pass string inside POST body.
它用于连接到POST方法URL

urlConnection.connect();

如果我在某个地方有错误的概念,请让我知道。

调试代码并检查代码流。您可以在get和post中使用此选项。您在服务器代码或结果中获得空值?,结果的值将是服务器响应的任何内容。@AbhishekK我在结果中得到null调试后,我知道我在结果字符串中得到null。调试代码并检查代码流后,您可以将其用于get和post。您在服务器代码或结果中得到null吗?,结果的值将是服务器响应的任何内容。@AbhishekK我在结果中得到null调试后,我知道我在结果字符串中得到null。