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和:我將我的大學項目方法不支持請求體:GET
Http get方法不支持有請求體,如果你想傳遞鍵值對,你需要將它們傳遞給Url – Bhargav
如果你刪除了'urlConnection.getOutputStream()。write(buf);'並嘗試不使用'setDoOutput'。 –
似乎你正試圖通過OutputStream「發佈」數據。檢查與更改setRequestMethod參數從「GET」到「POST」 –