Answer the question
In order to leave comments, you need to log in
How to get data from a method in Android in MainActivity?
Good day, dear community. I'm trying to implement one psychedelic idea (outputting random images on the screen), I'm facing this problem. When a static method is called in MainActivity, the application crashes. I feel like this is a really stupid mistake. Please tell me how to do it right and why? Thank you very much in advance for your reply.
Here is the code.
package com.eningapps.flickrrandomview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.flickr4java.flickr.Flickr;
import com.flickr4java.flickr.FlickrException;
import com.flickr4java.flickr.REST;
import com.flickr4java.flickr.photos.Photo;
import com.flickr4java.flickr.photos.PhotoList;
import com.flickr4java.flickr.photos.SearchParameters;
import com.squareup.picasso.Picasso;
public class MainActivity extends AppCompatActivity {
public static String getUrl() {
String[] SEARCHWORD={"dont worry"};
String apikey = "ТУТМОЙПАРОЛЬ";
String secret = "ТУТМОЙПАРОЛЬ";
Flickr flickr = new Flickr(apikey, secret, new REST());
SearchParameters searchParameters = new SearchParameters();
searchParameters.setAccuracy(1);
searchParameters.setTags(SEARCHWORD);
String rez="";
for (int i = 0; true; i++) {
try {
PhotoList<Photo> list = flickr.getPhotosInterface().search(searchParameters, 10, i);
//Глохнет конкретно в строке выше.Потому что идёт запрос на сервер, а я его выполняю в основном потоке?
if (list.isEmpty())
break;
for(Photo ph: list){
rez=ph.getLargeUrl();
if(!rez.equals("")) break;
}
} catch (FlickrException e) {
e.printStackTrace();
}
}
return rez;
}
ImageView iV;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iV=(ImageView) findViewById(R.id.imageView);
button=(Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Picasso.with(getApplicationContext())
.load(getUrl())
.resize(300, 300)
.into(iV); // а
}
});
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question