如下面代码:
package com.example.zz670.mypic;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.LinearLayout;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new work().start();
}
private class work extends Thread {
InputStream inputStream = null;
HttpURLConnection urlConnection = null;
String response;
String[] gURL = new String[10];
Bitmap bitmap;
ImageView imageView;
LinearLayout linearLayout;
public void run() {
try {
URL url = new URL("http://gank.io/api/random/data/福利/1");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(5000);
urlConnection.setRequestMethod("GET");
int statusCode = urlConnection.getResponseCode();
if (statusCode == 200) {
inputStream = new BufferedInputStream(urlConnection.getInputStream());
response = inputStream2String(inputStream);
}
if (!response.isEmpty()) {
try {
JSONObject jsonObject1 = new JSONObject(response);
// Log.e("Json", response);
JSONArray jsonArray = jsonObject1.getJSONArray("results");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = (JSONObject) jsonArray.get(i);
//取出name
String jarray1 = jsonObject.getString("url");
for (int j = 0; j < 10; j++) {
gURL[j] = jarray1;
Log.e("Json", jarray1.toString());
// Log.e("Json", gURL[j]);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
linearLayout = (LinearLayout) findViewById(R.id.id_gallery);
ImageView[] imageViews = new ImageView[10];
for (int i = 0; i < imageViews.length; i++) {
imageViews[i] = imageView = new ImageView(MainActivity.this);
try {
Bitmap bitmap = getBitmap(gURL[i]);
imageView.setImageBitmap(bitmap);
linearLayout.addView(imageView);
} catch (Exception e) {
}
}
}
String inputStream2String(InputStream is) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(is));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = in.readLine()) != null) {
buffer.append(line);
}
return buffer.toString();
}
private Bitmap getBitmap(String path) throws IOException {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if (conn.getResponseCode() == 200) {
inputStream = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
return null;
}
}
}
你是在线程里执行的 请求URL 路径,是异步的,
这里会先执行:
for (int i = 0; i < imageViews.length; i++) {
imageViews[i] = imageView = new ImageView(MainActivity.this);
try {
Bitmap bitmap = getBitmap(gURL[i]);
imageView.setImageBitmap(bitmap);
linearLayout.addView(imageView);
} catch (Exception e) {
}
}
所有 gURL[i]全是空,你可输出Log看一下。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。