我有json类来获取图像网址,但我不知道如何使用它 private String imageUrls []从静态链接获取图像动态URL。
*** string url是mainactivity中的静态链接,用json获取图片网址。
我的主要活动课程:
public class MainActivity extends Activity {
private final String url="http://192.168.1.4:81/upload/images.php";
ListView list;
LazyAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listView1);
adapter = new LazyAdapter(this, imageUrls);
list.setAdapter(adapter);
}
@Override
public void onDestroy() {
list.setAdapter(null);
super.onDestroy();
}
private String imageUrls[] = {
"http://www.technotalkative.com/wp-content/uploads/2012/09/tt_listview1-171x300.png",
"http://www.technotalkative.com/wp-content/uploads/2012/11/f-DayDream-Example-Landscape.png",
};
}
和json class:
class get url extends AsyncTask<String,Void,String>{
@Override
protected void onPreExecute() {
super.onPreExecute();
pd=new ProgressDialog(MainActivity.this);
pd.setMessage("login");
pd.show();
}
@Override
protected String doInBackground(String... params) {
List<NameValuePair> parms=new ArrayList<>();
JSONObject json=jParser.makeHTTPRequest(url,"GET");
try {
int t=json.getInt("t");
if(t==1){
s=json.getJSONArray("travel");
for(int i=0;i<s.length();i++){
String url_image=c.getString("url_image");
HashMap<String,String>map=new HashMap<String,String>();
map.put("url_image",url_image);
P.add(map);
}
}else {
Toast.makeText(MainActivity.this,"No Data Found",Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
答案 0 :(得分:0)
使用Gson库gor解析json对java对象的响应而不是手动提取字段要方便得多。使用Retrofit库而不是asynctasks也是一种很好的做法(更方便) - 请参阅https://stackoverflow.com/a/38002999/6175778 post