簡體   English   中英

Java的POST請求(AsyncTask)

[英]POST request with Java (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 在此處輸入圖片說明 另一方面卷曲一切都很好 在此處輸入圖片說明

有誰知道如何解決這個問題?

如果您從字符串中刪除引號,它將可以正常工作-這樣

.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- Web服務有問題

2-您發送了錯誤的參數(您的方法是發布)

如果想得到更好的字符串,請執行以下操作:

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");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM