发送HTTP POST - Android

时间:2015-06-30 05:22:26

标签: java android json url http-post

我知道有人问过这个问题,但大多数问题都已过时,方法已被弃用。我找到了这个解决方案,

new Thread( new Runnable() {
        @Override
        public void run() {
            try {
                String query = "param=" +"item"+"&other="+"num";

                URL url = new URL("http://www.url.com/url_post.php");
                HttpURLConnection connection = (HttpURLConnection)url.openConnection();
                //Set to POST
                connection.setDoOutput(true);
                connection.setRequestMethod("POST");
                connection.setReadTimeout(10000);
                Writer writer = new OutputStreamWriter(connection.getOutputStream());
                writer.write(query);
                writer.flush();
                writer.close();


            } catch (Exception e) {
                // TODO Auto-generated catch block
                Log.e(TAG, e.toString());
            }
        }
    }).start();

但是,它没有提供如何获取返回的数据,例如,我返回一些JSON,在哪里得到返回的数据?

感谢您的帮助:)

4 个答案:

答案 0 :(得分:0)

这样做

 //Get Response 
  InputStream is = connection.getInputStream();
  BufferedReader rd = new BufferedReader(new InputStreamReader(is));
  String line;
  StringBuffer response = new StringBuffer(); 
  while((line = rd.readLine()) != null) {
    response.append(line);
    response.append('\r');

最后作为回应,您将获得响应JSON字符串,然后执行您想要执行的操作。

有关详细信息,请访问此链接 http://www.xyzws.com/javafaq/how-to-use-httpurlconnection-post-data-to-web-server/139

答案 1 :(得分:0)

我建议您将此项目用作库:

https://github.com/matessoftwaresolutions/AndroidHttpRestService

这非常简单,我将它用于我的所有项目。我把它提交给了Github,因为我很难为一般目的找到一个简单的Rest客户端。

我将提交与Android Studio ASAP集成的更新。

我希望它有所帮助!!

答案 2 :(得分:0)

InputStream in = connection.getInputStream();
JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));

答案 3 :(得分:0)

在writer.close()

之后添加此部分
 connection.connect();
        int statusCode = connection.getResponseCode();
        Log.d("ON POST", " The status code is " + statusCode);

        if (statusCode == 200) {
            is = new BufferedInputStream(connection.getInputStream());
            String response = convertInputStreamToString(is);
            Log.d("ON POST", "The response is " + response);

            return response;

        } else {
            Log.d("ON POST", "On Else");
            return "";
        }

应创建ConvertInputStreamToString()以将json作为字符串

返回
public static String convertInputStreamToString(InputStream in) {
    BufferedReader reader = null;
    StringBuffer response = new StringBuffer();
    try {
        reader = new BufferedReader(new InputStreamReader(in));
        String line = "";
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return response.toString();
}