与标题一样,我尝试了很多来自网络的示例代码 但我不能成功。
这是我的MaiActivity.java
package com.example.user.bustime;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;
public class MainActivity extends ActionBarActivity {
final String url = "http://imp.5284.com.tw/TaipeiBusService/EstimateTime.aspx?DataFormat=json" ;
private Spinner spinner ;
private String time[];
private ArrayAdapter<String> adapterTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner)findViewById(R.id.spinner);
new GenerateBusData().execute();
}
public class GenerateBusData extends AsyncTask<Void,String,String>{
@Override
protected String doInBackground(Void... params) {
JSONParser jParser = new JSONParser();
try{
JSONObject json = jParser.getJSONFromUrl(url);
JSONArray i = json.getJSONArray("BusInfo");
time=new String[i.length()];
System.out.println("i.length()=>"+i.length());
for(int x=0;x<5;x++) {
Object jsonOb = i.get(x);
JSONObject t = new JSONObject(jsonOb.toString());
Object jsonOb1 = t.get("EstimateTime").toString();
time[x] = jsonOb1.toString();
}
}catch(JSONException e ){
e.printStackTrace();
}
// try{
// JSONObject j = readJsonFromUrl("http://imp.5284.com.tw/TaipeiBusService/EstimateTime.aspx?DataFormat=json");
// JSONArray i = j.getJSONArray("BusInfo");
// time=new String[i.length()];
// System.out.println("i.length()=>"+i.length());
// for(int x=0;x<10;x++){
//
// Object jsonOb = i.get(x);
// JSONObject t = new JSONObject(jsonOb.toString());
// Object jsonOb1 = t.get("EstimateTime").toString();
//
// time[x] = jsonOb1.toString();
// }
// }catch (Exception e){
// e.printStackTrace();
// }
return "";
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
String []tmp = {"a","b","c"};
adapterTime=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_dropdown_item,time);
adapterTime.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapterTime);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}
public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
InputStream is = new URL(url).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
JSONObject json = new JSONObject(jsonText);
return json;
} finally {
is.close();
}
}
}
JSONParser.java
package com.example.user.bustime;
import android.util.Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* Created by USER on 2015/2/1.
*/
public class JSONParser {
static InputStream iStream = null;
static JSONObject jsonOb = null;
static String json = "";
public JSONParser(){}
public JSONObject getJSONFromUrl(String url){
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try{
HttpResponse response= client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if(statusCode == 200){
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while((line = reader.readLine()) != null){
builder.append(line);
}
}else{
Log.e("==>", "Failed to download file");
}
}catch(ClientProtocolException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
try{
jsonOb = new JSONObject(builder.toString());
}catch(JSONException e){
Log.e("JSON Parser","Error parsing data "+e.toString());
}
return jsonOb;
}
}
我的json文件就像那样
{“EssentialInfo”:{“Location”:{“name”:“台北市”,“CenterName”:“台北市动态资讯中心”},“UpdateTime”:“2015-02-01 10:07: 14" , “CoordinateSystem”: “WGS84”}, “BusInfo”:[{ “StopID”:138730.0, “GoBack的”: “0”, “路由ID”:16588.0, “EstimateTime”: “ - 1”},{” StopID “:138731.0,” GoBack的 “:” 0" , “路由ID”:16588.0, “EstimateTime”: “ - 1”},{ “StopID”:138732.0, “GoBack的”: “0”, “路由ID”:16588.0, “EstimateTime”: “ - 1”},{ “StopID”:138733.0, “GoBack的”: “0”, “路由ID”:16588.0, “EstimateTime”: “ - 1”},{ “StopID”:138734.0,“GoBack的“:” 0" , “路由ID”:16588.0, “EstimateTime”: “ - 1”},{ “StopID”:138735.0, “GoBack的”: “0”, “路由ID”:16588.0, “EstimateTime”:“ - 1 “},{” StopID “:138736.0,” GoBack的 “:” 0" , “路由ID”:16588.0, “EstimateTime”: “ - 1”},{ “StopID”:138737.0, “GoBack的”: “0”,”路由ID “:16588.0,” EstimateTime “:” - 1 “},{” StopID “:138738.0,” GoBack的 “:” 0" , “路由ID”:16588.0, “EstimateTime”: “0”},{ “StopID”: 138739.0, “GoBack的”: “0”, “路由ID”:16588.0, “EstimateTime”: “120”},{ “StopID”:138740.0, “GoBack的”: “0”, “路由ID”:16588.0, “EstimateTime”: “120”},{ “StopID”:138741.0, “GoBack的”: “0”, “路由ID”:16588。 0 “EstimateTime”: “240”},{ “StopID”:138742.0, “GoBack的”: “0”, “路由ID”:16588.0, “EstimateTime”: “240”},{ “StopID”:138743.0,“GoBack的“:” 0" , “路由ID”:16588.0, “EstimateTime”: “0”}]}
答案 0 :(得分:-1)
你应该保存你的json数据&amp;把它放到像Dropbox这样的任何其他公共服务上,然后更改你的代码来访问该文件以查看它是否有效。
如果有效,您的问题可能在服务提供商(http://imp.5284.com.tw/TaipeiBusService/EstimateTime.aspx)中,而不是数据。
最后,你真的应该在这里发布你的错误信息,否则没有人会知道如何帮助你。
感谢。