问题
Someone know why not works this json in android? urlJson
Result is FileNotFoundException, but on the navigator works.
edit:
public static String readUrl(String urlString) throws Exception {
BufferedReader reader = null;
try{
URL url = new URL(urlString);
reader = new BufferedReader(new InputStreamReader (url.openStream()));
StringBuffer buffer = new StringBuffer();
int read;
char[]chars = new char[1024];
while ((read = reader.read(chars)) != -1)
buffer.append(chars, 0, read);
return buffer.toString();
} finally {
if (reader != null)
reader.close();
}
回答1:
Use BufferedReader is not the best method to do this task. Is a lot better use HttpClient with following sample using the library org.json.*;
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost("http://www.*****.com/json");
// Execute HTTP Post Request
HttpResponse response = httpClient.execute(post);
String respStr = EntityUtils.toString(response.getEntity());
JSONArray respJSON = new JSONArray(respStr);
回答2:
The format is wrong. Use {"name": "string", "...": "..."}
回答3:
Problem solved, I'm using spring and I removed this in the service
@RequestMapping(value="/categorias", method = RequestMethod.GET,headers="Accept=application/json")
public @ResponseBody Portada getCategorias() {
to
@RequestMapping(value="/categorias", method = RequestMethod.GET)
public @ResponseBody Portada getCategorias() {
Now works!
来源:https://stackoverflow.com/questions/11978412/reading-json-in-webservice-rest-in-android-filenotfoundexception