方法不支持请求体:GET

5
 class MySync extends AsyncTask{ ProgressDialog mProgressDialog;

    protected void onPreExecute(){
       mProgressDialog = ProgressDialog.show(MainActivity.this, "Loading...", "Data is Loading...");
    }
    @Override
    protected Integer doInBackground(String... params)  {
        int result = 0;
       //String url="http://192.168.0.108:8080/sbi/login?"+"key1="+params[0]+"&key2="+params[1]";
        int code;
        try {
            URL hp=new URL("http://192.168.0.108:8080/sbi/login?"+"key1="+params[0]+"&key2="+params[1]);
            HttpURLConnection urlConnection=(HttpURLConnection)hp.openConnection();
            urlConnection.connect();
            Log.i("A", "connect");
            code=urlConnection.getResponseCode();
            Log.i("A","code");
            boolean a=check(code);

           if(a){
                //urlConnection.setDoInput(true);
                Log.i("A", "input");
               // urlConnection.setDoOutput(true);
                Log.i("A", "output");
                urlConnection.setRequestMethod("GET");
                Log.i("A", "get");
                byte [] buf=("key1=" + params[0] + "&key2=" + params[1]).getBytes();
                urlConnection.getOutputStream().write(buf);
                Log.i("A", "sent");
            }
            else{
                Log.i("A","error");
                result=3;
            }
        }
        catch (MalformedURLException e) {
            Log.i("e", "Error");
        }
        catch (IOException e){
            e.printStackTrace();
        }
    protected boolean check(int c){
            if(c==200) return true;
            else return false;
        }
   }

该代码报错“方法不支持请求主体:get?”,如果我插入 setdooutput(true) ,那么它会报错“已连接”。我是 Android 的新手,正在制作我的大学项目。


Http get方法不支持请求体,如果您想使用get传递键值对,则需要将它们与URL一起传递。 - Bhargav
1
如果您删除 urlConnection.getOutputStream().write(buf); 并尝试不使用 setDoOutput,会发生什么? - Shree Krishna
看起来你正在尝试通过OutputStream“发布”数据。请检查将setRequestMethod参数从“GET”更改为“POST”。 - Anbarasu Chinna
它告诉java.net.ProtocolException:方法不支持请求体:POST - Mayank Singh
请告诉我如何通过键值对传递。 - Mayank Singh
2个回答

3
如果您真的想在请求体中向服务器发送键值对,则更改代码。
urlConnection.setRequestMethod("GET");

为了

urlConnection.setRequestMethod("POST");

如果服务器不支持POST但要求你使用GET,那么请删除这些行。
byte [] buf=("key1=" + params[0] + "&key2=" + params[1]).getBytes();
urlConnection.getOutputStream().write(buf);

从这行代码中,我可以看到...
URL hp=new URL("http://192.168.0.108:8080/sbi/login?"+"key1="+params[0]+"&key2="+params[1]);

你已经正确地构建了一个Url用于 GET的Http请求,但你却在不支持请求体的HTTP请求方法中添加一个请求体(在这种情况下是 GET http 方法)。
查看 这个维基百科页面 以获取更多关于REST和REST与HTTP/S的详细信息,以便更好地了解这种架构。

如果我想通过POST发送怎么办?你能提供一些基本的代码吗? - Mayank Singh
@MayankSingh 我已经在这里回答了,如果你想通过 POST 发送,请像我在答案中所说的那样更改请求方法 urlConnection.setRequestMethod("POST"); - Bhargav
一个不允许请求体的请求方法对我来说似乎毫无用处。我错过了什么?如何指定要选择哪个项目(在用于API查询时)? - Martin Pfeffer
根据HTTP标准的设计,GET请求不允许请求体。如果您想在GET请求中向服务器传递一些信息,您需要构造一个查询字符串,例如:https://blah.com/getsomething?id=12,name="martin",这样后端就能够从查询中接收这些参数。 - Bhargav

0
准备如下URL连接:
HttpURLConnection conn = null;
    try {
        conn = (HttpURLConnection) (url.openConnection());
    } catch (IOException e) {
        e.printStackTrace();
    }

调用

String dataToSend = "(\"key1=\" + params[0] + \"&key2=\" + params[1])";
conn.setRequestMethod("POST");// do not use "GET" in your case

conn.setRequestProperty("Content-Type", "application/json");//whatever you want

conn.setRequestProperty("Content-Length", "" + dataToSend.getBytes().length);

conn.setUseCaches(false);//set true to enable Cache for the req
conn.setDoOutput(true);//enable to write data to output stream
OutputStream os = conn.getOutputStream();
os.write(dataToSend.getBytes());
os.flush();
os.close();

别忘了在所有操作完成后调用以下方法

conn.connect();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接