Доброго времени суток, уважаемое сообщество. Пытаюсь реализовать одну психоделическую задумку (вывод рандомных картинок на экран), сталкиваюсь вот с какой проблемой.При вызове статического метода в MainActivity приложение падает. Чувствую, что это прям капец какая тупая ошибка. Скажите, пожалуйста, как сделать правильно и почему?
Заранее большое спасибо за ответ.
Вот код.Ниже есть сообщение ошибки.
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); // а
}
});
}
}
А вот сообщение ошибки.
org.scribe.exceptions.OAuthConnectionException: There was a problem while creating a connection to the remote service.
at org.scribe.model.Request.send(Request.java:70)
at org.scribe.model.Request.send(Request.java:76)
at com.flickr4java.flickr.REST.get(REST.java:175)
at com.flickr4java.flickr.photos.PhotosInterface.search(PhotosInterface.java:1047)
at com.eningapps.flickrrandomview.MainActivity.getUrl(MainActivity.java:35)
at com.eningapps.flickrrandomview.MainActivity$1.onClick(MainActivity.java:65)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:86)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74)
at java.net.InetAddress.getAllByName(InetAddress.java:752)
at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:187)
at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:156)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:98)
at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:345)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:328)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:246)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:457)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:126)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:89)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java)
at org.scribe.model.Response.<init>(Response.java:29)
at org.scribe.model.Request.doSend(Request.java:117)
at org.scribe.model.Request.send(Request.java:66)
at org.scribe.model.Request.send(Request.java:76)
at com.flickr4java.flickr.REST.get(REST.java:175)
at com.flickr4java.flickr.photos.PhotosInterface.search(PhotosInterface.java:1047)
at com.eningapps.flickrrandomview.MainActivity.getUrl(MainActivity.java:35)
at com.eningapps.flickrrandomview.MainActivity$1.onClick(MainActivity.java:65)