Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Solomon Alexandru
Solomon Alexandru
3,633 Points

Android Studio network error

I'm trying to create a simple client for tumblr using jumblr, a java client, and it seems that it doesn't work. It throws errors and I'm not sure what I am doing wrong! Could anyone please help me solve this?

Error:

03-31 09:25:57.033 1934-1934/com.example.genvera04.pyco E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: com.example.genvera04.pyco, PID: 1934
                                                                      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.genvera04.pyco/com.example.genvera04.pyco.MainActivity}: org.scribe.exceptions.OAuthConnectionException: There was a problem while creating a connection to the remote service.
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
                                                                          at android.app.ActivityThread.access$800(ActivityThread.java:144)
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                          at android.os.Looper.loop(Looper.java:135)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5221)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at java.lang.reflect.Method.invoke(Method.java:372)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
                                                                       Caused by: org.scribe.exceptions.OAuthConnectionException: There was a problem while creating a connection to the remote service.
                                                                          at org.scribe.model.Request.send(Request.java:69)
                                                                          at org.scribe.model.Request.send(Request.java:75)
                                                                          at com.tumblr.jumblr.request.RequestBuilder.get(RequestBuilder.java:92)
                                                                          at com.tumblr.jumblr.JumblrClient.user(JumblrClient.java:83)
                                                                          at com.example.genvera04.pyco.MainActivity.onCreate(MainActivity.java:43)
                                                                          at android.app.Activity.performCreate(Activity.java:5933)
                                                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
                                                                          at android.app.ActivityThread.access$800(ActivityThread.java:144) 
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                          at android.os.Looper.loop(Looper.java:135) 
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5221) 
                                                                          at java.lang.reflect.Method.invoke(Native Method) 
                                                                          at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
                                                                       Caused by: android.os.NetworkOnMainThreadException
                                                                          at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
                                                                          at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
                                                                          at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
                                                                          at java.net.InetAddress.getAllByName(InetAddress.java:215)
                                                                          at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29)
                                                                          at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)
                                                                          at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
                                                                          at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:272)
                                                                          at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
                                                                          at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:373)
                                                                          at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
                                                                          at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:89)
                                                                          at com.android.okhttp.internal.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:25)
                                                                          at org.scribe.model.Response.<init>(Response.java:28)
                                                                          at org.scribe.model.Request.doSend(Request.java:115)
                                                                          at org.scribe.model.Request.send(Request.java:65)
                                                                          at org.scribe.model.Request.send(Request.java:75) 
                                                                          at com.tumblr.jumblr.request.RequestBuilder.get(RequestBuilder.java:92) 
                                                                          at com.tumblr.jumblr.JumblrClient.user(JumblrClient.java:83) 
                                                                          at com.example.genvera04.pyco.MainActivity.onCreate(MainActivity.java:43) 
                                                                          at android.app.Activity.performCreate(Activity.java:5933) 
                                                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) 
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
                                                                          at android.app.ActivityThread.access$800(ActivityThread.java:144) 
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                          at android.os.Looper.loop(Looper.java:135) 
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5221) 
                                                                          at java.lang.reflect.Method.invoke(Native Method) 
                                                                          at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

And the MainActivity code:

package com.example.genvera04.pyco;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.tumblr.jumblr.JumblrClient;
import com.tumblr.jumblr.types.User;


public class MainActivity extends AppCompatActivity {
private final String CONSUMER_KEY = ""; 
private final String CONSUMER_SECRET = "";
private static String token_key = "";
private static String token_secret = "";
private JumblrClient client = new JumblrClient(CONSUMER_KEY, CONSUMER_SECRET);


private final String TAG = MainActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    JumblrClient client = new JumblrClient(CONSUMER_KEY, CONSUMER_SECRET);
    client.setToken(token_key, token_secret);

    User user = client.user();
    Log.e(TAG, user.getName());
}
}

2 Answers

Solomon Alexandru
Solomon Alexandru
3,633 Points

I saw that I have to use Asynctask for connecting to the internet, but I don't have any idea how to do it :(. Once I declare for example on "loadOnBackground" method, I can't use it on "onStart" because it is inside that other class. Can you please show me an example?

anil rahman
anil rahman
7,786 Points

I'm actually not that far into Android Studio myself so i wouldn't know how to code that. But try these links maybe you can use this code somehow? http://stackoverflow.com/questions/29709691/how-to-properly-use-asynctask-on-android http://stackoverflow.com/questions/21862435/internet-connection-detection-in-android-in-an-asynctask Or ask directly on stack overflow if no one seems to know.