2016-03-04 110 views
1
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

+0

Http get方法不支持有請求體,如果你想傳遞鍵值對,你需要將它們傳遞給Url – Bhargav

+1

如果你刪除了'urlConnection.getOutputStream()。write(buf);'並嘗試不使用'setDoOutput'。 –

+0

似乎你正試圖通過OutputStream「發佈」數據。檢查與更改setRequestMethod參數從「GET」到「POST」 –

回答

1

,如果你真的想鍵值對發送到請求主體服務器然後更改

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

您已經正確地建立網址爲GET Http請求,但您要添加的請求身體不支持請求主體HTTP請求方法參見(GET http方法)。

看看this wikipidea頁關於REST的詳細信息,並與HTTP REST/S,以獲得更詳細的想法在此架構

+0

如果我想通過發送通過張貼?請給我一些基本的代碼請 –

+0

@MayankSingh我已經回答說,在這裏,如果你想通過'POST'發送請求,就像我在回答'urlConnection.setRequestMethod(「POST」)中說的那樣改變請求方法;'' – 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(); 
相關問題