如何通过半参数requestUrl和Android其它的JSON请求?它的、参数、Android、requestUrl

2023-09-07 08:45:19 作者:爷来自地狱

我是新来的机器人,我已经取得了一个活动,我必须张贴一些参数,使API调用,并得到回应,我必须通过一些参数追加到请求的URL等为JSON格式,请告诉我我该怎么办,我的示例URL请求是如下:

http://dev.abctest.com/api/v1/book?customer_firstname=jigar&customer_lastname=jims&customer_mobile=9033309333&customer_email=jigar@epagestore.com&source_country=India&number_of_travellers=15

在机身的JSON

和其他参数如下图所示:

  {    目标:        {            city_id:1,            起始日期:2014年8月28日            END_DATE:2014年9月30日        },        {            city_id:5,            起始日期:2014年8月10日            END_DATE:2014年9月3日        }    ]} 

解决方案

首先,你需要在URL字段添加到您的基本URL。然后,您可以添加可选字段,如果您有任何。那么你的数据在HttpPost实体那里的URL将被处理后得到的之一。

尝试以下操作:

父方法被调用。

 公共无效请求(字符串的baseUrl,列表与LT;的NameValuePair> urlFields,列表与LT;的NameValuePair> FORMDATA,列表与LT;的NameValuePair> optionalData){//追加PARAMS到URL如果(urlFields!= NULL)    的baseUrl = +的baseUrl getUrlPathForGet(urlFields);//添加可选字段的网址如果(可选!= NULL)    的baseUrl =的baseUrl +? + URLEn codedUtils.format(optionalData,UTF-8);POSTDATA(的baseUrl,FORMDATA);} 
由于链接地址长度过长引起的 HTTP Error 400. The request URL is invalid 错误解决办法 修改注册表

这将网址参数附加到基础URL

 私人字符串getUrlPathForGet(列表<&的NameValuePair GT; urlFields){字符串路径=;如果(urlFields!= NULL){    对(对的NameValuePair:urlFields){    路径=路径+/+ pair.getValue();    }}返回路径;} 

表单数据作为实体添加到HttpPost对象与修改后的网址。

 公共无效POSTDATA(字符串的baseUrl,列表与LT;&的NameValuePair GT; FORMDATA){//创建一个新的HttpClient和邮政头HttpClient的HttpClient的=新DefaultHttpClient();//通过URL作为参数,并创建HttpPost对象。HttpPost后=新HttpPost(的baseUrl);//添加标题信息为您的要求 - 无需创建// BasicNameValuePair()和ArrayList。post.setHeader(授权,承载+令牌);post.setHeader(内容类型,应用/ JSON);post.setHeader(缓存控制,无缓存);尝试{//传递内容如下:post.setEntity(新UrlEn codedFormEntity(FORMDATA,                    HTTP.UTF_8));//执行HTTP POST请求HTT presponse响应= httpclient.execute(岗位);// TODO:处理你的回应,你想。}赶上(IOException异常五){    // TODO自动生成catch块}} 

I am new to android,I have made an activity in that i have to post some parameters to make api call and get response,I have to pass some parameters appending to request url and others as in Json format,Please tell me how can i do,My sample url request is as below:

http://dev.abctest.com/api/v1/book?customer_firstname=jigar&customer_lastname=jims&customer_mobile=9033309333&customer_email=jigar@epagestore.com&source_country=India&number_of_travellers=15

and other parameters in json body like below:

{

    "destinations": [
        {
            "city_id": 1,
            "start_date": "2014/08/28",
            "end_date": "2014/09/30"
        },
        {
            "city_id": 5,
            "start_date": "2014/08/10",
            "end_date": "2014/09/03"
        }
    ]
}

解决方案

First you need to append the url fields to your base url. Then you can add the optional fields if you have any. Then the your data as an entity in HttpPost where the url will be the one obtained after processing.

Try following :

The parent method to be called.

public void request(String baseUrl,List<NameValuePair> urlFields, List<NameValuePair> formData,List<NameValuePair> optionalData ){

// Append params to the URL 
if (urlFields != null)
    baseUrl = baseUrl + getUrlPathForGet(urlFields);

// adds Optional fields to the Url
if (optional != null)
    baseUrl = baseUrl + "?" + URLEncodedUtils.format(optionalData, "utf-8");

postData(baseUrl,formData);

}

It will append the url params to the base url

private String getUrlPathForGet(List<NameValuePair> urlFields) {

String path = "";

if (urlFields != null) {

    for (NameValuePair pair : urlFields) {
    path = path + "/" + pair.getValue();
    }
}

return path;
}

Add the form data as entity to HttpPost object with the modified url.

public void postData(String baseUrl,List<NameValuePair> formData) {

// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
// pass the url as parameter and create HttpPost object.
HttpPost post = new HttpPost(baseUrl);

// Add header information for your request - no need to create 
// BasicNameValuePair() and Arraylist.
post.setHeader("Authorization", "Bearer " + token);
post.setHeader("Content-Type", "application/json");
post.setHeader("Cache-Control", "no-cache");    

try {       

// pass the content as follows:
post.setEntity(new UrlEncodedFormEntity(formData,
                    HTTP.UTF_8));

// Execute HTTP Post Request
HttpResponse response = httpclient.execute(post);

// TODO: Process your response as you would like.

} catch (IOException e) {
    // TODO Auto-generated catch block
}
}