2016-05-03 3 views
-1

以下のコードは、パラメータ "myurl"を引数に取るメソッドdownloadUrl()を示しています。私がこれまでに送信する可能性のあるURLは2つだけです。メソッドの動作はそれぞれ異なります。POSTパラメータが空です。どうしたのですか? HttpURLConnection/Android/Java

myurl = URL1の場合、GETリクエストを使用するとすべて正常に動作します。

myurl = URL2の場合、POST要求を使用し、PHPページからの応答では、要求とともに送信されたPOSTパラメータが空であることを示します。私はPOSTのパラメータを設定する行を見ることができるので、なぜパラメータを送信しているのかわかりません!

ありがとうございました! -Adam。

private String downloadUrl(String myurl) throws IOException { 

     InputStream is = null; 

     String response = ""; 

     try { 
      URL urlObject = new URL(myurl); 
      HttpURLConnection conn = (HttpURLConnection) urlObject.openConnection(); 

      // find out if there's a way to incorporate these timeouts into the progress bar 
      // and what they mean for shitty network situations 


      conn.setReadTimeout(10000 /* milliseconds */); 
      conn.setConnectTimeout(15000 /* milliseconds */); 
      conn.setDoInput(true); 

      // INSERTED QUICK CHECK TO SEE WHICH URL WE ARE LOADING FROM 
      // it's important because one is GET, and one is POST 


      if (myurl.equals(url2)){ 

       Log.i(TAG, "dlurl() in async recognizes we are doing pre-call"); 



       conn.setRequestMethod("POST"); 
       conn.setDoOutput(true); 

       OutputStream os = conn.getOutputStream(); 
       BufferedWriter writer = new BufferedWriter(
         new OutputStreamWriter(os, "UTF-8")); 

       String postParams = "?phone=" + phone; 

       writer.write(postParams); 

       Log.i(TAG, "we're adding " + postParams + "to " + urlObject); 

       writer.flush(); 
       writer.close(); 
       os.close(); 


      } 

      else { 

       conn.setRequestMethod("GET"); 
       conn.connect(); 
      } 



      // Starts the query 

      int responseCode = conn.getResponseCode(); 
      Log.i(TAG, "from " + myurl + ", The response code from SERVER is: " + responseCode); 

      is = conn.getInputStream(); 

      // Convert the InputStream into a string 
      // i guess we look up how to do this 





      if (responseCode == HttpsURLConnection.HTTP_OK) { 
       String line; 
       BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
       while ((line = br.readLine()) != null) { 
        response += line; 
       } 
      } else { 
       response = "from downloadUrl, php page response was not OK: " + responseCode; 

      } 

      // it's good to close these things? 
      is.close(); 
      conn.disconnect(); 


      Log.i(TAG, "response is " + response); 

      return response; 

      // Makes sure that the InputStream is closed after the app is 
      // finished using it. 
     } finally { 
      if (is != null) { 
       is.close(); 
      } 
     } 
    } 
+1

あなたはconn.connect()を持っていません。それ以外の場合はそれがurl2と等しいとき –

+1

あなたの見出しは面白かったです。専門家にはAndroid/Javaが必要でした。仕事や何かを与えるように!もっと明確にする。 – HourGlass

+0

conn.connect()は問題ではありません...そして、タイトルについては申し訳ありません。ハハ。 –

答えて

1

POSTリクエストのパラメータを送信するには、次のコードブロックを試してみてください。

Map<String,String> params = new LinkedHashMap<>(); 
params.put("phone", "phone"); 

StringBuilder postPraamString = new StringBuilder(); 
for (Map.Entry<String,Object> param : params.entrySet()) { 
    if (postPraamString.length() != 0) postPraamString.append('&'); 
     postPraamString.append(URLEncoder.encode(param.getKey(), "UTF-8")); 
     postPraamString.append('='); 
     postPraamString.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8")); 
    } 
byte[] postDataBytes = postData.toString().getBytes("UTF-8"); 
writer.write(postDataBytes); 
+0

ありがとう!それも働いていただろうが、私は問題の根を見つけた。 –

0

だから私は...ラインで

の問題の根本を考え出し:

String postParams = "?phone=" + phone; 

問題は疑問符をリードするということでした。疑問符はGET要求でのみ使用する必要があります。

関連する問題