2013-10-10 64 views
4

我正在使用JSON Restful web服務器,我必須在服務URL中傳遞JSON對象。我已成功創建了JSON對象,但在URL創建與服務器的HTTP連接時發生異常。在Android中的RESTFUL Web服務的URL中傳遞JSON對象?

下面我有提到我的網址:

http://72.5.167.50:8084/UpdateProfileInfo?{"ProfileEditId":"917","ContactsEmail":[{"Email":"dsfs","ContactId":""}],"ContactsPhone":[{"CountryId":"+1","Type":"2","Phone":"345345"}],"ProfileId":"290","LastName":"demo","GroupId":"1212","Title":"sdf","City":"dsf","TemplateId":"1212","State":"dsf","AuthCode":"9bcc6f63-2050-4c5b-ba44-b8103fbc377a","Address":"sdf","FirstName":"demo","ContactId":"","Zip":"23","Company":"tv"} 

獲取java.lang.IllegalArgumentException: Illegal character in query代碼:

int TIMEOUT_MILLISEC = 100000; // 1000 milisec = 1 seconds 
int SOCKET_TIMEOUT_MILISEC = 120000; // 2 minutes 
HttpParams httpParams = new BasicHttpParams(); 
HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC); 
HttpConnectionParams.setSoTimeout(httpParams, SOCKET_TIMEOUT_MILISEC); 
HttpClient client = new DefaultHttpClient(httpParams); 
HttpPost request = new HttpPost(url); 
HttpResponse response = client.execute(request); 
responseString = request(response); 

請建議我,如果我做錯事與我的URL。

* 編輯: *試着用鑰匙仍然得到Exeception:

http://72.5.167.50:8084/UpdateProfileInfo?profileinof={"ProfileEditId":"917","ContactsEmail":[{"Email":"sdf","ContactId":""}],"ContactsPhone":[{"CountryId":"+1","Type":"2","Phone":"345345345"}],"ProfileId":"290","LastName":"demo","GroupId":"1212","Title":"dsf","City":"dsf","TemplateId":"1212","State":"dsf","AuthCode":"d968273a-0110-461b-8ecf-3f9c456d17ac","Address":"dsf","FirstName":"demo","ContactId":"","Zip":"23","Company":"tv"} 

回答

7

有,我們有必要做出這種請求的HTTP請求的格式不同。

我在下面提到了我的代碼。

public JSONObject getJSONObject(){ 


    return jsonObj; 
    } 

ABove方法返回一個JSON字符串,它在下面的方法中傳遞。

public static HttpResponse makeRequest(String url) throws Exception 
{ 
    //instantiates httpclient to make request 
    DefaultHttpClient httpclient = new DefaultHttpClient(); 

    //url with the post data 
    HttpPost httpost = new HttpPost(url); 

    //convert parameters into JSON object 
    JSONObject holder = getJSONObject(); 
    //passes the results to a string builder/entity 
    StringEntity se = new StringEntity(holder.toString()); 
    //sets the post request as the resulting string 
    httpost.setEntity(se); 
    httpost.setHeader("Accept", "application/json"); 
    httpost.setHeader("Content-type", "application/json"); 

    //Handles what is returned from the page 
    ResponseHandler responseHandler = new BasicResponseHandler(); 
    return httpclient.execute(httpost, responseHandler); 
} 

Stack post幫助我完成這項任務...... !!!

1

的IP是不正確的。

IP由4個字節組成。每個字節是從0到255的值,不能是7千。

的http:// .25.1617.50:1084

編輯:好吧,你編輯你的問題。你正在發送一個JSON作爲參數。但是這個參數沒有「鑰匙」。

應該是:

/UpdateProfileInfo? 信息 = { 「ProfileEditId」: 「917」,[......]

編輯:我覺得這應該是這樣的:

/UpdateProfileInfo? 信息 = 「{ 'ProfileEditId': '917',[.......]}」

注意,值由"包圍,並且所述內"'

現在換成
+0

的是由我改變..這是正確的..看到我編輯 –

+0

看到我編輯的答案 –

+0

@ Sam-In-TechValens我編輯了我的答案。再次檢查。無論如何,你應該使用由yanchenko提供的答案,因爲get參數有一個字符限制,而POST則不是。 – Reinherd

0

可能是問題在於您正試圖將JSON對象作爲url參數進行POST。
如果它真的必須是一個url參數,它必須是urlencoded
如果更應該是一個正常的POST請求,我的建議使用high level helper

new RESTClient2(ctx).post("http://72.5.167.50:8084", jsonObject); 
0

我可以看到需要使用POJO,將它們轉換爲JSON字符串並通過HTTP傳輸該字符串信息。有很多很好的android/java/apache/volley類型的庫允許這樣做。

但是,我不明白,其實我不同意你的要求使用GET和URL parms傳輸你的JSON字符串?

它很容易做到以下幾點:

POJO - >以JSON - >的toString - >到http.string.entity - > POST

爲什麼不重新審視自己的架構,並考慮使用POST不是GET。

那麼它的方便,2步:

看到example 「request.setEntity(...」

你的代碼看起來就像這樣:

httpPost.setEntity(new StringEntity(pojo.toJSON().toString())); 
+0

是的......那就是我所做的...已經回答了..謝謝:) –