2012-12-17 258 views
41

我正在使用下面的代碼發送一個HTTP POST請求,它將對象發送給WCF服務。這工作正常,但如果我的WCF服務還需要其他參數會發生什麼?我怎樣才能從我的Android客戶端發送它們?從Android發送JSON HTTP POST請求

這是我到目前爲止已經編寫的代碼:

StringBuilder sb = new StringBuilder(); 

String http = "http://android.schoolportal.gr/Service.svc/SaveValues"; 


HttpURLConnection urlConnection=null; 
try { 
    URL url = new URL(http); 
    urlConnection = (HttpURLConnection) url.openConnection(); 
    urlConnection.setDoOutput(true); 
    urlConnection.setRequestMethod("POST"); 
    urlConnection.setUseCaches(false); 
    urlConnection.setConnectTimeout(10000); 
    urlConnection.setReadTimeout(10000); 
    urlConnection.setRequestProperty("Content-Type","application/json"); 

    urlConnection.setRequestProperty("Host", "android.schoolportal.gr"); 
    urlConnection.connect(); 

    //Create JSONObject here 
    JSONObject jsonParam = new JSONObject(); 
    jsonParam.put("ID", "25"); 
    jsonParam.put("description", "Real"); 
    jsonParam.put("enable", "true"); 
    OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream()); 
    out.write(jsonParam.toString()); 
    out.close(); 

    int HttpResult =urlConnection.getResponseCode(); 
    if(HttpResult ==HttpURLConnection.HTTP_OK){ 
     BufferedReader br = new BufferedReader(new InputStreamReader( 
      urlConnection.getInputStream(),"utf-8")); 
     String line = null; 
     while ((line = br.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     br.close(); 

     System.out.println(""+sb.toString()); 

    }else{ 
      System.out.println(urlConnection.getResponseMessage()); 
    } 
} catch (MalformedURLException e) { 

     e.printStackTrace(); 
} 
catch (IOException e) { 

    e.printStackTrace(); 
    } catch (JSONException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
}finally{ 
    if(urlConnection!=null) 
    urlConnection.disconnect(); 
} 
+0

關注此鏈接。你正在嘗試發送其他參數,鏈接打擊會給你演示如何你可以在編碼後發送它們http://www.xyzws.com/Javafaq/how-to-use-httpurlconnection-post-data-to-web服務器/ 139 –

回答

55

發帖參數使用POST: -

​​

你錯過的部分是在事物的下面......即如下..

// Send POST output. 
printout = new DataOutputStream(urlConn.getOutputStream()); 
printout.writeBytes(URLEncoder.encode(jsonParam.toString(),"UTF-8")); 
printout.flush(); 
printout.close(); 

其餘的你可以做到。

+0

對不起,我接受你的答案,但現在我試圖執行它,我遇到了問題。你在哪裏傳遞你的參數? – Libathos

+0

jsonParam對象是發送參數。 – Harish

+0

好的,但我也想發送一個對象..我可以做到這一點?這意味着我現在必須發送2個json對象 – Libathos

13

嘗試一些東西一樣一擊:

SString otherParametersUrServiceNeed = "Company=acompany&Lng=test&MainPeriod=test&UserID=123&CourseDate=8:10:10"; 
String request = "http://android.schoolportal.gr/Service.svc/SaveValues"; 

URL url = new URL(request); 
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
connection.setDoOutput(true); 
connection.setDoInput(true); 
connection.setInstanceFollowRedirects(false); 
connection.setRequestMethod("POST"); 
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
connection.setRequestProperty("charset", "utf-8"); 
connection.setRequestProperty("Content-Length", "" + Integer.toString(otherParametersUrServiceNeed.getBytes().length)); 
connection.setUseCaches (false); 

DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); 
wr.writeBytes(otherParametersUrServiceNeed); 

    JSONObject jsonParam = new JSONObject(); 
jsonParam.put("ID", "25"); 
jsonParam.put("description", "Real"); 
jsonParam.put("enable", "true"); 

wr.writeBytes(jsonParam.toString()); 

wr.flush(); 
wr.close(); 

參考文獻:

  1. http://www.xyzws.com/Javafaq/how-to-use-httpurlconnection-post-data-to-web-server/139
  2. Java - sending HTTP parameters via POST method easily
+0

以及我做到了,但我仍然得到一個錯誤的請求作爲服務器響應。現在讓我問,如果我做了一切corect:當我設置其他參數,我應該把param1或我的參數的名稱?也,我的json對象是第一個參數,所以首先我寫這一個,然後我寫其他參數是正確的? – Libathos

+0

在服務器端param序列並不重要。看起來你把json放在有效載荷中...... 在這個'otherParametersUrServiceNeed =「param1 = a&param2 = b&param3 = c」;'把服務器需要的參數名稱設置爲只是示例..您應該首先確定您的服務器需要的參數,請求在'String otherParametersUrService'中添加這些參數和值,如我的示例所示。 –

+0

如果它不是機密的,你可以告訴我你的服務器需要什麼附加參數。所以我可以把它們放在上面的例子中。 –