在Android中的RESTFUL Web服务的URL中传递JSON对象?

时间:2013-10-10 13:05:56

标签: android json restful-url

我正在使用JSON Restful web服务,我必须在服务URL中传递JSON对象。我已成功创建了JSON对象,但在我的URL创建了与SERVER的HTTP连接时出现异常。

下面我提到了我的网址:

 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);

请建议我如果我的网址出错了。

* 已编辑: *尝试使用仍然获得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"}

4 个答案:

答案 0 :(得分:2)

我们需要为这种REQUEST做出不同格式的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 :(得分:1)

IP不正确。

IP由4个字节组成。每个字节都是0到255之间的值,不能是7千。

  

的http:// <强> 7232 .25.1617.50:1084

编辑:好的,你编辑了你的问题。您正在发送JSON作为参数。但是这个参数没有“关键”。

应该是:

  ?

/ UpdateProfileInfo的信息 = { “ProfileEditId”: “917”,[.......]

编辑:我认为这应该是这样的:

  

/ UpdateProfileInfo的信息 = “{ 'ProfileEditId': '917',[.......]}”?

请注意,该值已被"包围,内部"现在由'

替换

答案 2 :(得分:0)

问题可能是你试图将JSON对象作为url param发布。
如果它真的必须是一个网址参数,它必须是urlencoded 如果它应该是正常的POST请求,我建议使用high level helper

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

答案 3 :(得分:0)

我可以看到需要使用POJO,将它们转换为JSON字符串并通过HTTP传递该字符串信息。有很多很好的android / java / apache / volley类型的libs允许这样做。

但是,我不明白,实际上我不同意你使用GET和URL parms传输你的JSON字符串的要求?

很容易做到以下几点:

POJO - &gt;到JSON - &gt; toString - &gt;到http.string.entity - &gt; POST

为什么不重新检查您的架构并考虑使用POST而不是GET。

然后很简单,第2步:

请参阅example“request.setEntity(...”

您的代码将如下所示:

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