E
E
Ening_apps2017-09-20 15:42:34
Java
Ening_apps, 2017-09-20 15:42:34

How to get data from another method in Android main activity?

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. Below is the error message.

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); // а
            }
        });
    }
}

And here is the error message.
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) 

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
davidnum95, 2017-09-20
@davidnum95

Caused by: android.os.NetworkOnMainThreadException
The Internet request is made on the main thread, that's why it crashes

R
rainmatic, 2017-09-23
@rainmatic

Networking should not be done on the main (UI) thread. For your task it is better to use IntentService or AsyncTask. But without going into details, it's better to use an IntentService.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question