제목과 마찬가지로 웹에서 많은 예제 코드를 시도했습니다. 하지만 성공할 수 없습니다. { "위치": { "이름 웹에서 json 데이터를 가져올 수없는 이유
이
은 JSONParser.javapackage 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;
}
}
그리고
{"EssentialInfo "처럼 내 JSON 파일 내 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();
}
}
}
입니다 ","CentralName ":"CentralName ","UpdateTime ":"2015-02-01 10:07:14 ","CoordinateSystem ":"WGS84 "},"BusInfo ": [{ "StopID": 138730.0, "GoBack": "0", "RouteID": 16588.0, "EstimateTime": "- 1"}, { "StopID": 138731.0, "G "StopID": 138732.0, "GoBack": "0", "RouteID": 16588.0, "EstimateTime": "-"0 ","RouteID ": 16588.0,"EstimateTime " 1 "}, {"StopID ": 138733.0,"GoBack ":"0 ","RouteID ": 16588.0,"EstimateTime ":"-1 "}, {"StopID ": 138734.0,"GoBack " "RouteID": 16588.0, "EstimateTime": "-1"}, { "StopID": 138735.0, "GoBack": "0", "RouteID": 16588.0, "EstimateTime": "-1"}, { "StopID ": 138736.0,"GoBack ":"0 ","RouteID ": 16588.0,"EstimateTime ":"- 1 "}, {"StopID ": 138737.0,"GoBack ":"0 ","RouteID ": 16588.0, "StopID": 138738.0, "GoBack": "0", "RouteID": 16588.0, "EstimateTime": "0"}, { "StopID": 138739.0, "GoBack" "0", "RouteID": 16588.0, "EstimateTime": "120"}, { "StopID": 138740.0, "GoBack" "StopID": 138742.0, "GoBack": "0", "RouteID": 16588.0, "EstimateTime": "240"}, "StopID": 138741.0, "GoBack" "EstimateTime": "240"}, { "StopID": 138743.0, "GoBack": "0", "RouteID": 16588.0, "EstimateTime": "0"}]}
무엇이 오류입니까? – Rohit5k2
try catch에 모든 예외를 기록하고 여기에 게시하십시오. – iForests