今天,2015年10月29日。
在运行eclipse时,遇到如下问题:
1 com.alibaba.fastjson.JSONException: syntax error, expect {, actual error, pos 0 2 at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:212) 3 at com.alibaba.fastjson.parser.deserializer.ASMJavaBeanDeserializer.parseRest(ASMJavaBeanDeserializer.java:96) 4 at Fastjson_ASM_UpdateRequest_1.deserialze(Unknown Source) 5 at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:513) 6 at com.alibaba.fastjson.JSON.parseObject(JSON.java:244) 7 at com.alibaba.fastjson.JSON.parseObject(JSON.java:220) 8 at com.alibaba.fastjson.JSON.parseObject(JSON.java:179) 9 at com.alibaba.fastjson.JSON.parseObject(JSON.java:327)
看到这个,第一反应是解析的json格式不对,遂详细排查json格式问题。无奈怎么也找不到原因,在网上也没有找到解决方案。后无意间发现,原来是http请求方式错误。Get请求写成了Post请求。因此记录下这次的错误,以后不要再犯。
另附http请求之POSTandGET。
- POST篇:
1 private static Token getRemoteAccessToken() { 2 CloseableHttpClient httpClient = HttpClients.createDefault(); 3 HttpPost httppost = new HttpPost(URL); 4 httppost.setHeader("Content-Type", ContentType); 5 List<NameValuePair> params = new ArrayList<>(); 6 params.add(new BasicNameValuePair("grant_type", GrantTypeCredentials)); 7 params.add(new BasicNameValuePair("client_id", ClientId)); 8 params.add(new BasicNameValuePair("client_secret", ClientSecret)); 9 params.add(new BasicNameValuePair("scope", Scope)); 10 UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(params, Charset.forName("UTF-8")); 11 12 // 获取Token。 13 Token token = null; 14 httppost.setEntity(urlEncodedFormEntity); 15 // 发送 16 try (CloseableHttpResponse httpResponse = httpClient.execute(httppost)) { 17 HttpEntity entity = httpResponse.getEntity(); 18 if (entity != null) { 19 String entityString = EntityUtils.toString(entity, Charset.forName("UTF-8")); 20 token = JSON.parseObject(entityString, Token.class); 21 } 22 } catch (IOException e) { 23 LOGGER.error(ExceptionUtils.parseExceptionStackTrace(e)); 24 } finally { 25 try { 26 httpClient.close(); 27 } catch (IOException e) { 28 LOGGER.error(ExceptionUtils.parseExceptionStackTrace(e)); 29 } 30 } 31 return token; 32 }
- GET篇:
1 public Double getValue(String from,String to,Double value,String url) { 2 StringBuilder sbUri = new StringBuilder(url); 3 sbUri.append("?"); 4 sbUri.append("from=");sbUri.append(from); 5 sbUri.append("&"); 6 sbUri.append("to=");sbUri.append(to); 7 sbUri.append("&"); 8 sbUri.append("value=");sbUri.append(value.toString()); 9 10 //获取转换后value 11 CloseableHttpClient httpClient = HttpClients.createDefault(); 12 HttpGet httpGet = new HttpGet(sbUri.toString()); 13 httpGet.setHeader("Content-Type", ContentType); 14 15 UnitValue unitValue = null; 16 try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) { 17 HttpEntity entity = httpResponse.getEntity(); 18 if (entity != null) { 19 String entityString = EntityUtils.toString(entity, Charset.forName("UTF-8")); 20 unitValue = JSON.parseObject(entityString, UnitValue.class); 21 value=unitValue.getValue(); 22 } 23 } catch (IOException e) { 24 LOGGER.error(ExceptionUtils.parseExceptionStackTrace(e)); 25 } finally { 26 try { 27 httpClient.close(); 28 } catch (IOException e) { 29 LOGGER.error(ExceptionUtils.parseExceptionStackTrace(e)); 30 } 31 } 32 return value; 33 }