2011-12-26 15 views
0

http - postを呼び出そうとしています。私は彼にこの要求を送信 サーバが
1.最初のパラメータがintである2つのパラメータを持っている必要があり 2. 2番目のパラメータは、私は2つの方法でそれをやろうhttp投稿要求で2つのパラメータを送信できません

私は文字列として送信列挙し、それらの両方が失敗します:

最初の方法:

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
nameValuePairs.add(new BasicNameValuePair("intVal", "-100")); 
nameValuePairs.add(new BasicNameValuePair("enumVal", "enumAsString")); 
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

私は、応答として 'バー要求' を取得します。

第二の方法:私は、サーバがパラメータを取得することを参照してこのように

 DefaultHttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost(SERVER_ADDRESS + METHOD_NAME); 
try 
{ 
    HttpParams postParams = new BasicHttpParams(); 
      postParams.setIntParameter("intVal", -100); 
      postParams.setParameter ("enumVal" , "enumAsString"); 

      httppost.setParams(postParams); 

      HttpResponse p = httpclient.execute(httppost); 
     } 
     catch(Exception e) 
     { 

        .... 

     } 

- しかし、最初のパラメータ(int型もすることが必要)0で、2番目のパラメータ(列挙すべき必要性) 0である。

P.S:サーバー上で実行されるコードは、RESTを使用してWCFコードです。

..私はこの問題を解決する必要があります...

感謝。

+1

のですか?受信した内容を確認するためにデバッグを追加するにはアクセスできますか?あなたのコードが正しいデータを送信しているかどうかわからない場合は、何が返されたかを知ることは特に役に立ちません。 –

+0

サーバー上で実行されるコードはWCFコードです - RESTを使用 – Yanshof

+1

Webサーバーに要求を送信するために使用している完全なコードを表示します。 –

答えて

1

あなたはAndoridを使用してHttpPostHttpUrlConnectionでのparamを送ることができる2つの異なる方法である:

最初の方法:

HttpClient httpclient; 
HttpPost httppost; 
ArrayList<NameValuePair> postParameters; 
httpclient = new DefaultHttpClient(); 
httppost = new HttpPost("your login link"); 


postParameters = new ArrayList<NameValuePair>(); 
postParameters.add(new BasicNameValuePair("param1", "param1_value")); 
postParameters.add(new BasicNameValuePair("param2", "param2_value")); 

httppost.setEntity(new UrlEncodedFormEntity(postParameters)); 

HttpResponse response = httpclient.execute(httppost); 

第二の方法は次のとおりです。

 String charset = "UTF-8"; 
    String query = String.format("debug_data=%s&" 
          + "client_auth_hash=%s&" 
          + "timestamp=%s&" 
          + "client_api_ver=%s&", 
          URLEncoder.encode("1", charset), 
          URLEncoder.encode(hash, charset), 
          URLEncoder.encode(timeStamp, charset), 
          URLEncoder.encode(clientApiVersion, charset)); 

     System.setProperty("http.keepAlive", "false"); 
     HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); 
     connection.setDoOutput(true); 
     connection.setConnectTimeout(5000); //miliseconds 
     connection.setRequestMethod("POST"); 
     connection.setRequestProperty("Charset", charset); 
     connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset); 
     OutputStream output = null; 
     try { 
      output = connection.getOutputStream(); 
      output.write(query.getBytes(charset)); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      if (output != null) 
       try { 
        output.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
     } 

     int status = ((HttpURLConnection) connection).getResponseCode(); 
     Log.d("", "Status : " + status); 

     for (Entry<String, List<String>> header : connection 
       .getHeaderFields().entrySet()) { 
      Log.d("Headers", 
        "Headers : " + header.getKey() + "=" 
          + header.getValue()); 
     } 

     InputStream response = new BufferedInputStream(connection.getInputStream()); 

     int bytesRead = -1; 
     byte[] buffer = new byte[30 * 1024]; 
     while ((bytesRead = response.read(buffer)) > 0) { 
     //read the response in pieces if it's needed 
      byte[] buffer2 = new byte[bytesRead]; 
     } 
     connection.disconnect(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 

両方の方法が適切に機能しています。

2

これは私の場合には動作します。これを試してみて、私が起こるか知ってみましょう...ここで

List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(); 
nameValuePair.add(new BasicNameValuePair("uname","test")); 
nameValuePair.add(new BasicNameValuePair("pws","test")); 
HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost(url); 
httppost.addHeader("Content-Type", "application/x-www-form-urlencoded"); 
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8)); 

// Execute HTTP Post Request 
HttpResponse response = httpclient.execute(httppost); 
+0

この方法は機能しません。 – Yanshof

0

をwarking方法から[OK]を、何もこれは、サーバー上で実行中のどのような私はこの問題を解決する方法とその作業罰金

JSONObject json = new JSONObject(); 
json.put("intVal", -100); 
json.put("enumVal", enumAsString); 


StringEntity t = new StringEntity(json.toString(), "UTF-8"); 

httppost.setEntity(t); 


httppost.setHeader("Accept", "application/json"); 
httppost.setHeader("Content-type", "application/json"); 
関連する問題