1

我正在使用 JSON Restful web 服务,我必须在服务 URL 中传递 JSON 对象。我已成功创建 JSON 对象,但当我的 URL 创建与服务器的 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);

如果我的网址有问题,请建议我。

*已编辑: *尝试使用仍然获得异常的密钥:

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

4 回答 4

2

我们需要为这种请求制作不同格式的 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);
}

堆栈帖子帮助我完成了这项任务......!!!

于 2013-10-10T14:13:18.410 回答
1

The IP is not correct.

IP is formed with 4 bytes. Every byte is a value from 0 to 255, can't be 7 thousand.

http://7232.25.1617.50:1084

Edit: Okay, you edited your question. You're sending a JSON as parameter. But this parameter has no "key".

Should be:

/UpdateProfileInfo?info={"ProfileEditId":"917",[.......]

Edit: I think this should be like this:

/UpdateProfileInfo?info="{'ProfileEditId':'917',[.......]}"

Notice that the value is surrounded by ", and the inner " are replaced now by '

于 2013-10-10T13:11:31.750 回答
0

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

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

执行以下操作非常容易:

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

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

然后很简单,2步:

参见示例“request.setEntity(...”

您的代码将如下所示:

httpPost.setEntity(new StringEntity(pojo.toJSON().toString()));
于 2013-10-10T14:17:20.157 回答
0

问题可能是您试图将 JSON 对象作为 url 参数发布。
如果它真的必须是一个 url 参数,那么它必须是urlencoded
如果它应该是一个普通的 POST 请求,我建议使用高级助手

new RESTClient2(ctx).post("http://72.5.167.50:8084", jsonObject);
于 2013-10-10T13:17:28.687 回答