2017-01-13 45 views
-1

請告訴我寄與GET方法PARAMS在HttpURLConnection的.. 我使用這個代碼的方式..但我不知道我應該用什麼方法來發送PARAMS如何使用GET方法發送PARAMS在HttpURLConnection的Android中

is = connection.getInputStream();

這裏

它投擲例外FileNotFoundException是代碼..

//is this line is correct? 
    url = new URL(u + "?" + getQuery(params)); 
        Log.v("testing", url.toString()); 

        connection = (HttpURLConnection) url.openConnection(); 
        connection.setReadTimeout(10000); 
        connection.setConnectTimeout(15000); 
        connection.setRequestMethod("GET"); 
        connection.setDoInput(true); 
        connection.setDoOutput(true); 

        os = connection.getOutputStream(); 
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os)); 
        //bw.write(getQuery(params)); 
        bw.flush(); 
        bw.close(); 
        os.close(); 

        connection.connect(); 

        is = connection.getInputStream(); 
        String line = ""; 
        BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
        StringBuilder sb = new StringBuilder(); 
        while ((line = br.readLine()) != null){ 
         sb.append(line); 
        } 
+1

'u +「?」 + getQuery(params)'。足夠了。這就是你必須要做的。刪除作家的東西。並沒有輸出承諾。不要混淆輸出流。 – greenapps

回答

0

我得到了解決。從代碼中刪除輸出的東西..就是這樣

url = new URL(u + "?" + getQuery(params)); 
       Log.v("testing", url.toString()); 

       connection = (HttpURLConnection) url.openConnection(); 
       connection.setReadTimeout(10000); 
       connection.setConnectTimeout(15000); 
       connection.setRequestMethod("GET"); 
       connection.setDoInput(true); 

//code removed from here 

       connection.connect(); 

       is = connection.getInputStream(); 
       String line = ""; 
       BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
       StringBuilder sb = new StringBuilder(); 
       while ((line = br.readLine()) != null){ 
        sb.append(line); 
       } 
+0

'我得到了解決方案.'。奇怪的溝通方式。你甚至沒有對我的評論作出反應。你只應該確認我的評論。不發佈完整答案。 – greenapps

+0

我試圖找到評論的投票選項,但我沒有找到這就是爲什麼我發佈它 – Asad

相關問題