2013-02-19 97 views
1

在我的應用我想與像用戶ID發送參數的JSONObject,任何人都可以請建議我從那裏我可以採取一些幫助一些教程...發送JSON對象與參數

我得到這個代碼,但在這代碼我只能夠發送的JSONObject不是參數..

我的網址是這樣的:http://www.example.com/JSONObject/userId

我如何可以傳遞的JSONObject與用戶id到服務器..

public class HTTPPoster { 
    public HttpResponse doPost(String url, JSONObject jsonProfilo) throws ClientProtocolException, IOException { 
     DefaultHttpClient httpclient = new DefaultHttpClient(); 
     HttpPost request = new HttpPost(url); 
     HttpEntity entity; 
     StringEntity s = new StringEntity(jsonProfilo.toString()); 
     s.setContentEncoding((Header) new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); 
     entity = s; 
     request.setEntity(entity); 
     HttpResponse response; 
     response = httpclient.execute(request); 
     return response; 
    } 
} 

謝謝..

回答

0

你是什麼意思「我只能發送JSONObject而不是參數。」 ,你可以把你所有的參數中的JSONObject本身,我沒有得到什麼是你真正的問題

0

在JSONObject的,你可以保持您的參數,如:

JSONObject jsonvalue =new JSONObject(); 
jsonvalue.put("userId", "urUserId"); 

示例代碼,這裏IM發送服務器imeiNumber ,userName和paasword:

JSONObject jsonvalue =new JSONObject(); 
jsonvalue = new JSONObject(); 
jsonvalue.put("imeiNumber", "00000000000"); 
jsonvalue.put("userName", mUserName); 
jsonvalue.put("plainPassword", mPassword); 

我的觀點是類似的,你可以發送userId和globalId到服務器。

現在在烏拉圭回合的HttpConnection:

HttpURLConnection conn = null; 
conn = (HttpURLConnection) url.openConnection(); 
conn.setRequestProperty("Content-Type", "application/json"); 
conn.setDoOutput(true); 
conn.setRequestMethod("POST"); 
conn.setChunkedStreamingMode(0); 
conn.setUseCaches(false); 
String urlParameters = "body" 
    + URLEncoder.encode(jsonvalue.toString(), "UTF-8");  
conn.setRequestProperty("Content-Length", 
    "" + Integer.toString(urlParameters.getBytes().length)); 

try { 
    OutputStream os = conn.getOutputStream(); 
    os.write(jsonvalue.toString().getBytes()); 
    os.close(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 
+0

就會把我的用戶id中的JSONObject做的工作?因爲我的URL是這樣的:www.example.com/api/JSONObject/userId – 2013-02-19 09:34:54

+0

ofcourse u可以保留用戶ID和其他細節在你的JSON ....它將完美工作 – user1969053 2013-02-19 09:38:35

+0

我有一個字與我的PHP開發人員,他說我需要userId獨立作爲一個字符串不在jsonobject – 2013-02-19 09:41:12