当前位置: 代码迷 >> Android >> android使用post连服务器有关问题
  详细解决方案

android使用post连服务器有关问题

热度:83   发布时间:2016-05-01 17:56:37.0
android使用post连服务器问题
代码如下:
 public static String from(String servletName, String pageNow, String recId,
  String recComId, String recJobName, String recComIndustry,
  String recAddress, String recTime, String keyWord) { // 使用该方法从服务器获取json字符串
  String jsonArr = "";
  try {
  HttpPost request = new HttpPost(URL + servletName); //提示这行报错啊 List<NameValuePair> params = new ArrayList<NameValuePair>();
 
  if (pageNow != null) {
  BasicNameValuePair pagenow = new BasicNameValuePair("pagenow",
  pageNow);
  params.add(pagenow);
  }
  if (recId != null) {
  BasicNameValuePair recid = new BasicNameValuePair("recid",
  recId);
  params.add(recid);
  }
  if (recComId != null) {
  BasicNameValuePair reccomid = new BasicNameValuePair(
  "reccomid", recComId);
  params.add(reccomid);
  }
  if (recJobName != null) {
  BasicNameValuePair recjobname = new BasicNameValuePair(
  "recjobname", recJobName);
  params.add(recjobname);
  }
  if (recComIndustry != null) {
  BasicNameValuePair reccomindustry = new BasicNameValuePair(
  "reccomindustry", recComIndustry);
  params.add(reccomindustry);
  }
  if (recAddress != null) {
  BasicNameValuePair recaddress = new BasicNameValuePair(
  "recaddress", recAddress);
  params.add(recaddress);
  }
  if (recTime != null) {
  BasicNameValuePair rectime = new BasicNameValuePair("rectime",
  recTime);
  params.add(rectime);
  }
  if (keyWord != null) {
  BasicNameValuePair keyword = new BasicNameValuePair("keyword",
  keyWord);
  params.add(keyword);
  }
 
  request.setEntity(new UrlEncodedFormEntity(params, "GBK"));
  HttpResponse response = new DefaultHttpClient().execute(request);
  if (response.getStatusLine().getStatusCode() != 404) {
  jsonArr = EntityUtils.toString(response.getEntity()).trim();
  }
  } catch (UnsupportedEncodingException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  相关解决方案