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