Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 方法不支持请求主体:GET_Java_Android - Fatal编程技术网

Java 方法不支持请求主体:GET

Java 方法不支持请求主体:GET,java,android,Java,Android,此代码给出的错误方法不支持请求主体:get?另外,如果我插入setdooutput(true),那么它会给出已经连接的错误。我是android的新手,我正在做我的大学项目若你们真的想把键值对发送到请求正文中的服务器,那个么就改变 class MySync extends AsyncTask{ ProgressDialog mProgressDialog; protected void onPreExecute(){ mProgressDialog = ProgressD

此代码给出的错误方法不支持请求主体:get?另外,如果我插入setdooutput(true),那么它会给出已经连接的错误。我是android的新手,我正在做我的大学项目

若你们真的想把键值对发送到请求正文中的服务器,那个么就改变

 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;
        }
   }

或者,如果服务器不支持
POST
,但要求您执行
GET
,则删除行

urlConnection.setRequestMethod("POST");
从这一行我可以看到

byte [] buf=("key1=" + params[0] + "&key2=" + params[1]).getBytes();
urlConnection.getOutputStream().write(buf);
您已经为
GET
Http请求正确地构建了Url,但是您正在将请求正文添加到不支持请求正文的Http请求方法中(
GET
Http方法在本例中)


查看页面了解有关REST和REST with HTTP/S的更多详细信息,以获得有关此体系结构的更详细信息

如下所示准备URL连接

URL hp=new URL("http://192.168.0.108:8080/sbi/login?"+"key1="+params[0]+"&key2="+params[1]);
召唤

毕竟不要忘记调用下面的方法

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

Http get方法不支持有请求体,若您想用get传递键值对,则需要用urlConnection.getOutputStream().write(buf)传递它们,若您删除
urlConnection.getOutputStream(),该怎么办
并尝试不使用
setDoOutput
。似乎您正试图通过OutputStream“发布”数据。通过将setRequestMethod参数从“GET”更改为“POST”进行检查,它告诉java.net.ProtocolException:method不支持请求主体:POST请告诉我如何传递键值对如果我想通过POST发送什么?你能给我一些基本代码吗please@MayankSingh我在这里已经回答过了,如果您想通过
POST
发送,只需像我在回答
urlConnection.setRequestMethod(“POST”)中所说的那样更改请求方法不允许请求主体的请求方法在我看来毫无用处。我错过了什么?如何指定要选择的项(当用于api查询时)?
GET
请求通过HTTP标准的设计不允许请求正文,如果您想在
GET
请求中向服务器传递一些信息,您需要构造一个查询字符串,如
https://blah.com/getsomething?id=12,name=“martin”
这样后端就可以从查询中接收这些参数。
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();