解决
我试图从json url中保存img url,我有点卡在这里。请帮忙 我是android开发的新手,很抱歉,如果这是一个noob问题。谢谢!
public class images {
private ArrayList<String> url;
private String TAG = "Array list";
public images() {
url = new ArrayList<>();
new setupJson().execute("https://wallapp.000webhostapp.com");
}
public ArrayList<String> getImages() {
return url;
}
public String getOneImage(int position) {
return url.get(position);
}
public class setupJson extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... strings) {
// Http connection
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL urlConn = new URL(strings[0]);
connection = (HttpURLConnection) urlConn.openConnection();
connection.connect();
InputStream istream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(istream));
StringBuffer buffer = new StringBuffer();
String line = "";
while((line = reader.readLine()) != null) {
buffer.append(line+"\n");
}
return buffer.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(connection != null) {
connection.disconnect();
}
try {
if(reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// Log.e(TAG,"Result: "+result);
try {
JSONObject jsonObj = new JSONObject(result);
// Getting JSON Array node
JSONArray imgs = jsonObj.getJSONArray("img");
for (int i = 0; i < imgs.length(); i++) {
JSONObject url_img = imgs.getJSONObject(i);
String get_url = url_img.getString("url");
//String get_cat = url_img.getString("category");
url.add(get_url);
}
} catch (final JSONException e)
{
Log.e(TAG, "Json parsing error: " + e.getMessage());
}
}
在for循环之后,如果我记录url变量,它会显示值,但之后 new setupJson()。execute(); 当我记录url时它显示为空(即[])
感谢@Selvin我弄清楚我修复的代码是什么问题,将setupJson类移动到mainactivity和onPostExecute我设置了适配器。
MainAcitivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridview);
new setupJson().execute();
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String image_url = image.getOneImage(i);
Intent intent = new Intent(MainActivity.this,WallImage.class);
intent.putExtra("url",image_url);
startActivity(intent);
//Toast.makeText(getApplicationContext(),image_url,Toast.LENGTH_LONG).show();
}
});
}
public class setupJson extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
String url = "https://wallapp.000webhostapp.com";
String jsonStr = sh.makeServiceCall(url);
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray imgs = jsonObj.getJSONArray("img");
for (int i = 0; i < imgs.length(); i++) {
JSONObject url_img = imgs.getJSONObject(i);
String get_url = "https://wallapp.000webhostapp.com/"+url_img.getString("url");
//String get_cat = url_img.getString("category");
image.url.add(get_url);
}
} catch (final JSONException e)
{
Log.e(TAG, "Json parsing error: " + e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
gridView.setAdapter(new ImageAdapter(MainActivity.this));
}
}
}