我是android的初学者,我试图调用simple weather api来返回JSON响应。
我想知道进行api调用的有效方法是什么,以及从哪里学习这些方法。
我尝试使用Jackson实现它:
WebService.java (从某个链接复制粘贴)
public class WebService {
public <T> T get(String url, T object) throws Exception {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.set("x-system-code", "mobile");
headers.set("x-server-api-password", "mobile");
headers.set("x-server-api-key", "mobile");
headers.set("Connection", "Close");
HttpEntity entity = new HttpEntity("parameters", headers);
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
return (T) restTemplate.exchange(url, HttpMethod.GET, entity, object.getClass()).getBody();
}
public <T> T get(String url, T object, Map uriVariables) throws Exception {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.set("Connection", "Close");
HttpEntity entity = new HttpEntity("parameters", headers);
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
return (T) restTemplate.exchange(url, HttpMethod.GET, entity, object.getClass(), uriVariables).getBody();
}
public <T> T post(String url, Object request, T object) throws Exception {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.set("x-system-code", "mobile");
headers.set("x-server-api-password", "mobile");
headers.set("x-server-api-key", "mobile");
headers.set("Connection", "Close");
HttpEntity entity = new HttpEntity(request, headers);
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
return (T) restTemplate.exchange(url, HttpMethod.POST, entity, object.getClass()).getBody();
}
public void put(String url, Object request) {
try {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
restTemplate.put(url, request);
} catch (Exception e) {
Log.e("WebService - put", e.getMessage(), e);
}
}
public void delete(String url, Object request) {
try {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
restTemplate.delete(url);
} catch (Exception e) {
Log.e("WebService - delete", e.getMessage(), e);
}
}
}`
MainActivity.java
public class MainActivity extends AppCompatActivity {
WebService webService=new WebService();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GetWeather getWeather=new GetWeather();
getWeather.execute();
}
public class GetWeather extends AsyncTask<URL, Integer, response>{
String url;
public GetWeather(){
url="http://api.openweathermap.org/data/2.5/weather?q=Gurgaon&units=metric&appid=94d605bef2c15307c23f65a326d*****";
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected response doInBackground(URL... params) {
try {
response weatherResponse= webService.get(url,new response());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(response response) {
super.onPostExecute(response);
Log.i("response", response.toString());
}
}
}
错误
org.springframework.web.client.ResourceAccessException:I / O错误:连接上的意外流结束{api.openweathermap.org:80,proxy = DIRECT hostAddress = 128.199.109.89 cipherSuite = none protocol = http / 1.1} (回收计数= 0);嵌套异常是java.io.IOException:连接上的意外结束流{api.openweathermap.org:80,proxy = DIRECT hostAddress = 128.199.109.89 cipherSuite = none protocol = http / 1.1}(recycle count = 0) 01-04 07:21:51.536 21098-21137 / com.example.rajatgupta.weatherapp W / System.err:at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:491)
请告诉我我做错了什么,或者任何其他方式使API命中。
答案 0 :(得分:1)
我建议您在Android开发者网站和整个网络上查看Volley大量优秀教程。
使用示例:
JsonObjectRequest jsonRequest = new JsonObjectRequest(GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Log.d("JSON", response.toString());
//Do something with the data
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("JSON", error.toString());
}
});
VolleyRequestQueue.getInstance(getActivity()).addToRequestQueue(jsonRequest);
答案 1 :(得分:0)
查看Retrofit,它是一个非常简单和紧凑的前向库,可以帮助您实现REST API的接口。