用picasso加载string变量加载不出来,换成url直接敲上去就可以,为什么?
package com.example.zz670.mypic;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.squareup.picasso.Picasso;
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 {
ImageView imageView;
LinearLayout linearLayout;
ImageView[] imageViews = new ImageView[10];
static String gURL;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = (LinearLayout) findViewById(R.id.id_gallery);
new work().start();
try {
Thread.sleep(1000);
for (int i = 0; i < imageViews.length; i++) {
imageView = new ImageView(this);
try {
// final Bitmap bitmap = getBitmap(gURL[i]);
//Log.e("ceshi","4-"+i);
//imageView.setImageBitmap(bitmap);
Log.e("ceshi","5-"+i);
Log.e("ceshi",gURL);
Picasso.with(MainActivity.this).load(gURL).into(imageView);
//就在这里,gURL字符串已经赋值为"xxxxx.jpg",但是不能加载,不报错
//但是直接把"xxxxx.jpg"敲上去就可以加载出来
//已经声明网络权限
linearLayout.addView(imageView);
} catch (Exception e) {
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private class work extends Thread {
InputStream inputStream = null;
HttpURLConnection urlConnection = null;
String response;
//Bitmap bitmap;
public void run() {
try {
//Log.e("ceshi","1");
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);
}
//Log.e("ceshi","2");
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");
gURL = jarray1;
//Log.e("Json", jarray1.toString());
Log.e("Json", gURL);
}
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Log.e("ceshi","3");
}
public 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();
}
}
}
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.squareup.picasso.Picasso;
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 {
ImageView imageView;
LinearLayout linearLayout;
ImageView[] imageViews = new ImageView[10];
static String gURL;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = (LinearLayout) findViewById(R.id.id_gallery);
new work().start();
try {
Thread.sleep(1000);
for (int i = 0; i < imageViews.length; i++) {
imageView = new ImageView(this);
try {
// final Bitmap bitmap = getBitmap(gURL[i]);
//Log.e("ceshi","4-"+i);
//imageView.setImageBitmap(bitmap);
Log.e("ceshi","5-"+i);
Log.e("ceshi",gURL);
Picasso.with(MainActivity.this).load(gURL).into(imageView);
//就在这里,gURL字符串已经赋值为"xxxxx.jpg",但是不能加载,不报错
//但是直接把"xxxxx.jpg"敲上去就可以加载出来
//已经声明网络权限
linearLayout.addView(imageView);
} catch (Exception e) {
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private class work extends Thread {
InputStream inputStream = null;
HttpURLConnection urlConnection = null;
String response;
//Bitmap bitmap;
public void run() {
try {
//Log.e("ceshi","1");
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);
}
//Log.e("ceshi","2");
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");
gURL = jarray1;
//Log.e("Json", jarray1.toString());
Log.e("Json", gURL);
}
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Log.e("ceshi","3");
}
public 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();
}
}
}
...全文
相关推荐
Android 使用Picasso加载网络图片等比例缩放的实现方法
在做android图片加载的时候,由于手机屏幕受限,很多大图加载过来的时候,我们要求等比例缩放,接下来小编给大家带来了Android 使用Picasso加载网络图片等比例缩放的实现方法,感兴趣的朋友一起看看吧
Andorid开发之Picasso通过URL获取用户头像的圆形显示
主要介绍了Andorid开发之Picasso通过URL获取用户头像的圆形显示,非常不错,具有参考借鉴价值,需要的朋友可以参考下
Picasso的简单使用
Picasso是一个强大的图片下载缓存框架,这里列举了几个经常用到的使用方式
Picasso二次封装
原文:http://blog.csdn.net/qq_26787115/article/details/51746953
Android Picasso使用高斯模糊处理的示例代码
本篇文章主要介绍了Android Picasso使用高斯模糊处理的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧


