我正在创建一个Android应用程序,在应用程序数据中从JSON文件中读取,使用列表视图显示标题,当用户单击标题时,它们将被带到包含其余数据的第二个屏幕。我在一个方面遇到问题,在JSON文件中是一个名为image的部分,它显示了URL的结尾(全部存储在变量中),如何将这个位加到URL1来创建完整的图像网址,下载图像并将其显示在主屏幕上标题的左侧,由于前两个JSON项目没有图像,我将如何从系统加载默认值(捕获错误)?
它需要从第三个项目开始获取图像,因为前两个没有图像字段。
我已经在下面添加了我的代码和JSON文件的结构。
public class MainActivity extends AppCompatActivity {
private String TAG = MainActivity.class.getSimpleName();
private ProgressDialog pDialog;
private ListView lv;
// URL to get Android Version Data JSON
private static String url = "http://codetest.cobi.co.za/androids.json";
private static String url1 = "http://codetest.cobi.co.za/";
ArrayList<HashMap<String, String>> androidversions;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
androidversions = new ArrayList<>();
lv = (ListView) findViewById(R.id.list);
new GetVersions().execute();
}
private class GetVersions extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray versions = jsonObj.getJSONArray("versions");
// looping through All Versions
for (int i = 0; i < versions.length(); i++) {
JSONObject c = versions.getJSONObject(i);
String name = c.getString("name");
String version = c.getString("version");
String released = c.getString("released");
String api = c.getString("api");
String image = c.getString("image");
// tmp hash map for single version
HashMap<String, String> Version = new HashMap<>();
// adding each child node to HashMap key => value
Version.put("name", name);
Version.put("version", version);
Version.put("released", released);
Version.put("api", api);
Version.put("image",image);
// adding Data to version list
androidversions.add(Version);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
HashMap<String, String> version = androidversions.get(position);
String name = version.get("name");
String ver = version.get("version");
String released = version.get("released");
String api = version.get("api");
Intent intent = new Intent(MainActivity.this, Second_Level.class);
intent.putExtra("name",name);
intent.putExtra("version",ver);
intent.putExtra("released",released);
intent.putExtra("api",api);
MainActivity.this.startActivity(intent);
}
});
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, androidversions,
R.layout.list_item, new String[]{"name"}, new int[]{name});
lv.setAdapter(adapter);
}
}
}
布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="75dp"
android:layout_height="75dp"
android:id="@+id/image"/>
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:paddingTop="6dip"
android:textColor="@color/colorPrimaryDark"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/version"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:textColor="@color/colorAccent" />
<TextView
android:id="@+id/released"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5d5d5d"
android:textStyle="bold" />
<TextView
android:id="@+id/api"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5d5d5d"
android:textStyle="bold" />
</LinearLayout>
JSON结构
"versions": [
{
"name":"Alpha",
"version":"1.0",
"released":"September 23, 2008",
"api":"1"
},
{
"name":"Beta",
"version":"1.1",
"released":"February 9, 2009",
"api":"2"
},
{
"name":"Cupcake",
"version":"1.5",
"released":"April 27, 2009",
"api":"3",
"image":"images/cupcake.jpg"
答案 0 :(得分:0)
假设有一个相对路径,只需附加字符串
String image = url1 + c.getString("image");
顺便说一下,您应该在onCreate
内设置点击监听器和适配器,而不是onPostExecute
。