gpt4 book ai didi

android - 如何在 android 的 JSON 请求中传递 requestUrl 和其他参数中的一半参数?

转载 作者:行者123 更新时间:2023-11-29 17:47:55 25 4
gpt4 key购买 nike

我是 android 的新手,我做了一个 Activity ,我必须发布一些参数来进行 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 正文中的其他参数如下:

{

"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"
}
]
}

最佳答案

  1. 使用 JsonParserHelper 作为 Utility 类。每次点击 url 时。

a.) 上一个类并在该课上分隔您的网址假设 App_WebServiceUrls

public class App_WebServiceUrls {

public static String GetDetails ="http://dev.abctest.com/api/v1/book";

}

2.现在调用webservice/Web Api时。在单独的线程中调用 api 或使用 Asynctasks所以要避免 NetworkOnMainThredException。

new Thread(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub

// Add your data
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();

nameValuePair.add(new BasicNameValuePair(customer_firstname, "Deepak"));
nameValuePair.add(new BasicNameValuePair(customer_lastname, "Panwar"));


JSONObject json = null;

try {

json = new JSONObject();

json = JsonParserHelper.makeHttpRequest(
App_WebServiceUrls.CompanyDivisions, "GET", nameValuePair);

Log.d("Division List Response:", "" + json);

if (json != null) {
}else
{

/**To print tost on ui thread**/
runOnUiThread(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub

/**Write Toast here**/

}
});

} catch (JSONException e) {
e.printStackTrace();
}


}
}).start();

/调用 webapi 的帮助类/

public class JsonParserHelper {

static InputStream is = null;

static JSONObject jObj = null;

static JSONArray jArr = null;

static String json = "";

public static JSONObject makeHttpRequest(String url, String method,

List<NameValuePair> params) {

try {

if (method == "POST") {

DefaultHttpClient httpClient = new DefaultHttpClient();

String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;

Log.v("Urltocheck", "" + url);

HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();

is = httpEntity.getContent();

} else if (method == "GET") {
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();

String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;

Log.v("Urltocheck", "" + url);

HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}

// try parse the string to a JSON object
try {
// jArr = new JSONArray(json);

jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}

// return JSON String (Array)
// return jArr;

return jObj;

}

}

关于android - 如何在 android 的 JSON 请求中传递 requestUrl 和其他参数中的一半参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25117708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com