使用Java的POST请求(AsyncTask)

时间:2017-09-20 18:00:00

标签: java android curl android-asynctask

我正在尝试使用Java发出HTTP Post请求,这里是代码

protected class DownloadInfoOfWeather extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {

        final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36";
        String result = "";

        try {
            URL url = new URL(params[0]);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.setRequestMethod("POST");
            connection.addRequestProperty("User-Agent", USER_AGENT);
            connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

            connection.setDoOutput(true);
            DataOutputStream write = new DataOutputStream(connection.getOutputStream());

            write.writeBytes(params[1]);
            write.flush();
            write.close();

            // Response: 400
            Log.e("Response", connection.getResponseCode() + "");

        } catch (Exception e) {
            Log.e(e.toString(), "Something with request");
        }

        return null;
    }
}

public void clickHelloWorld (View view) {

    DownloadInfoOfWeather downloadInfoOfWeather = new DownloadInfoOfWeather();

    String url = "https://query.yahooapis.com/v1/public/yql";
    String body = "q=\"select wind from weather.forecast where woeid=2460286\"&format=json";

    downloadInfoOfWeather.execute(url, body);

}

当我运行此代码时,我得到响应:400; 我使用Yahoo API enter image description here 另一方面,卷曲一切正常enter image description here

有谁知道如何解决这个问题?

2 个答案:

答案 0 :(得分:4)

如果您从字符串中删除引号,它就可以正常工作 - 就像那样

.execute(
"https://query.yahooapis.com/v1/public/yql",
"q=select wind from weather.forecast where woeid=2460286&format=json")

我还清理了一点连接代码

protected static class DownloadInfoOfWeather extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {

        try {
            URL url = new URL(params[0]);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestProperty("Accept", "*/*");

            connection.setDoOutput(true);
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
            writer.write(params[1]);
            writer.close();

            connection.connect();

            // Response: 400
            Log.e("Response", connection.getResponseMessage() + "");

        } catch (Exception e) {
            Log.e(e.toString(), "Something with request");
        }

        return null;
    }
}

答案 1 :(得分:0)

这意味着1-网络服务有问题

2 - 您发送了错误的参数(您的方法是发布的)

如果你想得到Strings beter,请执行以下操作:

protected class DownloadInfoOfWeather extends AsyncTask<void//notice, Void, String> 
make a counstructor :
DownloadInfoOfWeather(Strings 1 , Strings 2){

}

then : 

new Asyntask(Strings).execute();
//+ this           
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");