Reputation: 165
For some reason whenever I try to load a URL gotten from a GET request to a server it won't load but If i try to load the string directly it works. Here is my code:
new Thread(new Runnable() {
@Override public void run() {
try {
URL obj = new URL(url1 + overallid);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.addRequestProperty("User-Agent", "Mozilla/4.76");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString().replaceAll("\\s", ""));
System.out.println("Set pic to: " + pic);
Picasso.with(LoginActivity.this).load(pic).into(image);
i++;
overallid++;
} catch (Exception ex) {
System.out.println(ex);
}
}
}).start();
If I make pic = a imgur link straight up it works but if I grab it from the GET it doesn't work. Any ideas?
Thanks, Quinn(Fusion)
Upvotes: 0
Views: 470
Reputation: 12147
Here is an example to handle the bitmap downloading.
BufferedInputStream inputStream = null;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
URL url = new URL("the image url to load");
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
inputStream = new BufferedInputStream(connection.getInputStream());
byte[] buffer = new byte[8192];
int n = -1;
while ((n = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, n);
}
final Bitmap bitmap = BitmapFactory.decodeByteArray(
outputStream.toByteArray(), 0, outputStream.size());
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO handle image here
}
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Upvotes: 1
Reputation: 1841
Picasso should be called from the Main Thread... try out this code:
Handler mainThreadHandler=new Handler();
new Thread(new Runnable() {
@Override
public void run() {
try {
URL obj = new URL(url1 + overallid);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.addRequestProperty("User-Agent", "Mozilla/4.76");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString().replaceAll("\\s", ""));
System.out.println("Set pic to: " + pic);
mainThreadHandler.post(new Runnable() {
@Override
public void run() {
Picasso.with(LoginActivity.this).load(pic).into(image);
}
});
i++;
overallid++;
} catch (Exception ex) {
System.out.println(ex);
}
}
}).start();
Upvotes: 1