gpt4 book ai didi

android - 解析以该对象的最后一个值开头的多个 json 对象值。

转载 作者:行者123 更新时间:2023-11-29 16:02:15 24 4
gpt4 key购买 nike

我正在开发一个应用程序,我正在从我的远程服务器获取响应(Json)。我也能够根据该响应解析和放置标记。但是我无法解析来自响应的 ph 数据 并将其传递到另一个 Activity 中。

在 setOnInfoWindowClickListener 事件中仅发送最后一个 json 对象数据的电话号码

我知道我必须做一些小的修改。请在这方面给我建议。

这是我收到的 Json 响应。

[
{
id: 965,
distance: "1.47",
ph: "33441111",
name: "XYZ",
LS: " abcdef",
point: "77.588018,12.959282"
},
{
id: 965,
distance: "1.47",
ph: "33441111",
name: "XYZ",
LS: " abcdef",
point: "77.588018,12.959282"
},
.
.
]

我试过这种方式解析

private class HttpGetTask extends AsyncTask<Void, Void, String> {

// Showing progress dialog
// Passing URL

@Override
protected String doInBackground(Void... params) {

// http stuff
}

@Override
protected void onPostExecute(String result) {

if (pDialog.isShowing())
pDialog.dismiss();
try {
JSONArray json = new JSONArray(result);

for (int i = 0; i < json.length(); i++) {
Log.v("Response", result);
final JSONObject e = json.getJSONObject(i);
String point = e.getString("point");

final String phone = e.getString("ph");

String[] point2 = point.split(",");
double lat1 = Double.parseDouble(point2[0]);
double lng1 = Double.parseDouble(point2[1]);

gMap.addMarker(new MarkerOptions()
.title(e.getString("name"))
.snippet(
e.getString("LS") + "*" + e.getString("ph"))
.position(new LatLng(lng1, lat1))
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.pmr)));

gMap.setInfoWindowAdapter(new InfoWindowAdapter() {

@Override
public View getInfoWindow(Marker arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public View getInfoContents(Marker mrkr) {
// TODO Auto-generated method stub
String name = mrkr.getTitle();
String detail = mrkr.getSnippet();
String trimmedDetail = detail.substring(0, 60);

Log.v("Info", name + " " + detail);
View v = getLayoutInflater().inflate(
R.layout.infowindow, null);
TextView title = (TextView) v
.findViewById(R.id.titleTV);
TextView snippet = (TextView) v
.findViewById(R.id.snippetTV);

title.setText("" + name);
snippet.setText("" + trimmedDetail);

return v;
}
});

gMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

@Override
public void onInfoWindowClick(Marker arg0) {

Intent myIntent = new Intent(getBaseContext(),
DtActivity.class);
myIntent.putExtra("title", arg0.getTitle());
myIntent.putExtra("detail", arg0.getSnippet());
myIntent.putExtra("ph1", phone);

// How to access and send ph here.

try {
String ph = e.getString("ph");
myIntent.putExtra("ph", ph);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
startActivity(myIntent);
}
});

}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

if (null != mClient)
mClient.close();

}
}

最佳答案

您需要初始化 gMap Globaly 并在 for 循环中添加标记。从表单循环中删除适配器和信息窗口单击监听器代码并将其粘贴到外部。请检查以下示例。

public class Example extends FragmentActivity implements OnInfoWindowClickListener {

public static GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapview);
mMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();

mMap.setOnInfoWindowClickListener(this);
// Inside the Loop

mMap.addMarker(new MarkerOptions().title(e.getString("name"))
.snippet(e.getString("LS") + "*" + e.getString("ph"))
.position(new LatLng(lng1, lat1))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.pmr)));
// Close the loop

}

@Override
public void onInfoWindowClick(Marker marker) {
// here you can get title, snippet, lat,lng of selected marker. then you
// can start activity.
}

请将 for 循环粘贴到 Asyntask 类的 onPostExecute 中。

关于android - 解析以该对象的最后一个值开头的多个 json 对象值。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23209649/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com