我一直在寻找,但我想我需要个人帮助。我有一个名为" FriendsList"的变量。其中包含3个名字," Andrew"," Bro"," Jeremy"。我试图通过每个名字,让那些人经度和纬度,并将其添加到地图。 到目前为止,我有这个,但我认为JSON是错误的,因为我得到了错误。
for(int i =0; i < FriendsList.size(); i++)
{
//start the post to the database
String responseBody1 = null;
httpClient = new DefaultHttpClient();
httpPost = new HttpPost("http://skyrealmstudio.com/GetSpecificUserLocation.php");
List<NameValuePair> nameValuePair1 = new ArrayList<NameValuePair>();
String friendToFind = FriendsList.get(i).toString();
nameValuePair1.add(new BasicNameValuePair("Username", friendToFind));
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
response = httpClient.execute(httpPost);
responseBody1 = EntityUtils.toString(response.getEntity());
// writing response to log
Log.d("Http Response:", response.toString());
} catch (IOException e) {
e.printStackTrace();
}
//end the post response
//JSON the string that is got from the post.
jsonStr = responseBody1;
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null){
jsonArr = new JSONArray(jsonStr);
JSONObject c = jsonArr.getJSONObject(i);
tempMarker = new MarkerOptions()
.position(new LatLng(Double.parseDouble(c.getString(TAG_LATITUDE)), Double.parseDouble(c.getString(TAG_LONGITUDE))))
.title(friendToFind)
.snippet(c.getString(TAG_COMMENTS));
markers.add(i, tempMarker);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
public void onPostExecute(Void Result)
{
for(int i = 0; i < FriendsList.size(); i++)
{
googleMap.getMap().addMarker(new MarkerOptions().title(markers.get(i).getTitle())
.position(new LatLng(markers.get(i).getPosition().latitude, markers.get(i).getPosition().longitude)));
}
}
我从这段代码中得到的错误如下:
6882-6882/com.skyrealm.brockyy.findmypeepsapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.skyrealm.brockyy.findmypeepsapp, PID: 6882
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.skyrealm.brockyy.findmypeepsapp.MainActivity$getFriendsList.onPostExecute(MainActivity.java:631)
at com.skyrealm.brockyy.findmypeepsapp.MainActivity$getFriendsList.onPostExecute(MainActivity.java:520)
at android.os.AsyncTask.finish(AsyncTask.java:636)
at android.os.AsyncTask.access$500(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
我注意到错误说索引1是一个无效的索引,这使我相信它正在解析不正确的东西。
答案 0 :(得分:0)
而不是这段代码:
for(int i = 0; i < FriendsList.size(); i++)
{
googleMap.getMap().addMarker(new MarkerOptions().title(markers.get(i).getTitle())
.position(new LatLng(markers.get(i).getPosition().latitude, markers.get(i).getPosition().longitude)));
}
试试这个:
for(int i = 0; i < markers.size(); i++)
{
googleMap.getMap().addMarker(new MarkerOptions().title(markers.get(i).getTitle())
.position(new LatLng(markers.get(i).getPosition().latitude, markers.get(i).getPosition().longitude)));
}