在Android中使用POST方法后如何获得响应

时间:2017-04-21 09:28:47

标签: android androidhttpclient

网址为:http://localhost 方法类型:发布 header:Content-Type:application / json,decode:2 数据:xx

我们怎么能在android中实现这个?,我怎么得到这个? 我看到Httpclient已被弃用

任何帮助将不胜感激

4 个答案:

答案 0 :(得分:2)

尝试使用HttpUrlConnection

String Url, query;

    InputStream inputStream;
    HttpURLConnection urlConnection;
    byte[] outputBytes;
    String ResponseData;
    Context context;
try{
            URL url = new URL(Url);
            urlConnection = (HttpURLConnection) url.openConnection();
            outputBytes = query.getBytes("UTF-8");
            urlConnection.setRequestMethod("POST");
            urlConnection.setDoOutput(true);
            urlConnection.setConnectTimeout(15000);
            urlConnection.setRequestProperty("Content-Type", "application/json");
            urlConnection.connect();

            OutputStream os = urlConnection.getOutputStream();
            os.write(outputBytes);
            os.flush();
            os.close();

            inputStream = new BufferedInputStream(urlConnection.getInputStream());
            ResponseData = convertStreamToString(inputStream);

    } catch (MalformedURLException e) {

                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();

            }


    public String convertStreamToString(InputStream is) {

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append((line + "\n"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

答案 1 :(得分:1)

 URL url = new URL("http://yoururl.com");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("firstParam", paramValue1));
params.add(new BasicNameValuePair("secondParam", paramValue2));
params.add(new BasicNameValuePair("thirdParam", paramValue3));
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
        new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(params));
writer.flush();
writer.close();
os.close();
conn.connect();

............................................... ...................

private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException
{
    StringBuilder result = new StringBuilder();
    boolean first = true;

    for (NameValuePair pair : params)
    {
        if (first)
            first = false;
        else
            result.append("&");

        result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
        result.append("=");
        result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
    }

    return result.toString();
}

答案 2 :(得分:0)

如果您不想使用库,可以使用HttpURLConnection,否则您也可以使用Volley进行网络通话,因为排球可以更轻松地管理网络通话! 谢谢!

答案 3 :(得分:0)

您可以使用Retrofit库,因为Retrofit比其他方法更快,更轻松地加载来自HTTP请求的响应。 访问https://square.github.io/retrofit/

例如,这里给出了示例Json对象,

$file

代码:

$file->isValid()