このWebサイトでJSONオブジェクト値を解析しようとしています..それでも、ランタイムエラーが発生します:アクティビティコンポーネント情報を開始できません: URLに不正な文字..これは私のコードです:
public class MainActivity extends ListActivity {
private static final String TAG_ROWS = "row";
private static final String TAG_ELEMENTS = "elements";
private static final String TAG_DISTANCE = "distance";
private static final String TAG_VALUE = "value";
private static final String TAG_ADDRESS = "destination_addresses";
String data;
HttpClient client;
double minDistance=0;
static JSONObject jObj = null;
String destination_addresses;
JSONArray rows;
JSONArray elements;
String value;
final static String URL= "http://maps.googleapis.com/maps/api/distancematrix/json?";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
try {
ClosestObject();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
finally {
}
setContentView(R.layout.activity_main);
TextView result = (TextView) findViewById(R.id.text1);
result.setText(data);
}
public void ClosestObject () throws JSONException, ClientProtocolException, IOException {
// Creating JSON Parser instance
String param= "origins=Vancouver+BC&destinations=burnaby|Victoria+BC|Surrey+BC&mode=driving&language=en-FR&sensor=false";
String encodedparams = URLEncoder.encode(param,"UTF-8");
String FinalUrl= URL +encodedparams;
StringBuilder url = new StringBuilder(FinalUrl);
HttpGet get = new HttpGet(url.toString());
HttpResponse r = client.execute(get);
HttpEntity en = r.getEntity();
data = EntityUtils.toString(en);
jObj = new JSONObject(data);
try {
rows = jObj.getJSONArray(TAG_ROWS);
elements = rows.getJSONArray(0);
for (int i=0; i<elements.length();i++){
JSONObject obj = elements.getJSONObject(i);
JSONObject distance = obj.getJSONObject(TAG_DISTANCE);
value= distance.getString(TAG_VALUE);
double distanceValue = Double.parseDouble(value);
if ( distanceValue < minDistance){
minDistance = distanceValue;
}
}
}
catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
これはエラーです 。 group=0x40a71930) 04-01 21:06:05.764: E/AndroidRuntime(1045): 致命的な例外: メイン 04-01 21:06:05.764: E/AndroidRuntime(1045): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.distance/com.example.distance.MainActivity}: java.lang.IllegalArgumentException: インデックス 97 のクエリで無効な文字: http://maps.googleapis.com/maps/api/distancematrix/json?origins =バンクーバー+BC&destinations=バーナビー|ビクトリア+BC|サリー+BC&mode=driving&language=en-FR&sensor=falsehttp://maps.googleapis.com/maps/api/distancematrix/json?origins=バンクーバー+BC&destinations=バーナビー|ビクトリア+BC |サリー+BC&モード=運転&language=en-FR&sensor=false 04-01 21:06:05.764: E/AndroidRuntime(1045): android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 04-01 21:06:05.764: E/AndroidRuntime(1045): Android で。 app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 04-01 21:06:05.764: E/AndroidRuntime(1045): android.app.ActivityThread.access$600(ActivityThread.java:141) 04-01 21:06 :05.764: E/AndroidRuntime(1045): android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 04-01 21:06:05.764: E/AndroidRuntime(1045): android.os.Handler で。 dispatchMessage(Handler.java:99) 04-01 21:06:05.764: E/AndroidRuntime(1045): android.os.Looper.loop(Looper.java:137) 04-01 21:06:05.764: E/ AndroidRuntime(1045): android.app.ActivityThread.main(ActivityThread.java:5041) 04-01 21:06:05.764: E/AndroidRuntime(1045): java.lang.reflect で。Method.invokeNative(ネイティブ メソッド) 04-01 21:06:05.764: E/AndroidRuntime(1045): java.lang.reflect.Method.invoke(Method.java:511) 04-01 21:06:05.764: E /AndroidRuntime(1045): com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 04-01 21:06:05.764: E/AndroidRuntime(1045): com.android.internal. os.ZygoteInit.main(ZygoteInit.java:560) 04-01 21:06:05.764: E/AndroidRuntime(1045): dalvik.system.NativeStart.main(ネイティブ メソッド) 04-01 21:06:05.764: E /AndroidRuntime(1045): 原因: java.lang.IllegalArgumentException: インデックス 97 のクエリに無効な文字:java:793) 04-01 21:06:05.764: E/AndroidRuntime(1045): com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 04-01 21:06:05.764: E/ AndroidRuntime(1045): dalvik.system.NativeStart.main(Native Method) 04-01 21:06:05.764: E/AndroidRuntime(1045): 原因: java.lang.IllegalArgumentException: インデックス 97 のクエリで無効な文字:java:793) 04-01 21:06:05.764: E/AndroidRuntime(1045): com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 04-01 21:06:05.764: E/ AndroidRuntime(1045): dalvik.system.NativeStart.main(Native Method) 04-01 21:06:05.764: E/AndroidRuntime(1045): 原因: java.lang.IllegalArgumentException: インデックス 97 のクエリで無効な文字:http://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver+BC&destinations=burnaby|ビクトリア+BC|サリー+BC&mode=driving&language=en-FR&sensor=falsehttp://maps.googleapis.com/ maps/api/distancematrix/json?origins=Vancouver+BC&destinations=burnaby|Victoria+BC|Surrey+BC&mode=driving&language=en-FR&sensor=false 04-01 21:06:05.764: E/AndroidRuntime(1045): java.net.URI.create(URI.java:727) 04-01 21:06:05.764: E/AndroidRuntime(1045): org. apache.http.client.methods.HttpPost.(HttpPost.java:79) 04-01 21:06:05.764: E/AndroidRuntime(1045): com.example.distance.MainActivity$JSONParser.getJSONFromUrl(MainActivity.java: 68) 04-01 21:06:05.764: E/AndroidRuntime(1045): com.example.distance.MainActivity.ClosestObject(MainActivity.java:115) 04-01 21:06:05.764: E/AndroidRuntime(1045) : com.example.distance.MainActivity.onCreate(MainActivity.java:44) 04-01 21:06:05.764: E/AndroidRuntime(1045): android.app.Activity.performCreate(Activity.java:5104) 04 -01 21:06:05.764: E/AndroidRuntime(1045): android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 04-01 21:06:05.764: E/AndroidRuntime(1045): Android で。app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 04-01 21:06:05.764: E/AndroidRuntime(1045): ... 11 もっと見る
私を助けてくれてありがとう:)