0

I'm trying to load a remote file and I have to use HttpClient.execute which always throws an error. Any ideas?

Function I'm using:

private void loadCol3(Map.Entry<String, String> map) {
    LinearLayout col3 = (LinearLayout)findViewById(R.id.col3);
    String result = null;
    col3.removeAllViews();

    Log.d(TAG, "start load col 3");

    HttpClient httpclient = new DefaultHttpClient();
    HttpGet request = new HttpGet("http://video.foxbusiness.com/v/feed/playlist/"+map.getValue()+".js?template=fox&callback=?");
    ResponseHandler<String> handler = new BasicResponseHandler();  

    try {  
        result = httpclient.execute(request, handler);  
    } catch (Exception e) {
        Log.d(TAG, "httpclient.execute failed");
        e.printStackTrace();  
    }
    httpclient.getConnectionManager().shutdown();

    try {
        JSONObject completeJSONObj = new JSONObject(result);
        String json = completeJSONObj.toString();
        Log.d(TAG, json);
        JSONObject results = completeJSONObj.getJSONObject("results");
    } catch (JSONException e) {
        Log.d(TAG,"JSON parsing error:" + e.getMessage());
    }
}

manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.tv"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="11" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <activity android:name=".MyActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

Current log when ran:

07-31 17:56:17.175: DEBUG/log_tag(10869): start load col 3
07-31 17:56:17.205: DEBUG/log_tag(10869): httpclient.execute failed

Last line of console:

[2011-07-31 11:57:57 - My app] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.tv/.MyActivity }

** edit - stacktraces **

07-31 18:11:47.475: WARN/System.err(11083): android.os.NetworkOnMainThreadException
07-31 18:11:47.475: WARN/System.err(11083):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077)
07-31 18:11:47.475: WARN/System.err(11083):     at java.net.InetAddress.lookupHostByName(InetAddress.java:477)
07-31 18:11:47.475: WARN/System.err(11083):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:277)
07-31 18:11:47.475: WARN/System.err(11083):     at java.net.InetAddress.getAllByName(InetAddress.java:249)
07-31 18:11:47.475: WARN/System.err(11083):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136)
07-31 18:11:47.475: WARN/System.err(11083):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-31 18:11:47.475: WARN/System.err(11083):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-31 18:11:47.475: WARN/System.err(11083):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
07-31 18:11:47.475: WARN/System.err(11083):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-31 18:11:47.475: WARN/System.err(11083):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:653)
07-31 18:11:47.475: WARN/System.err(11083):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
07-31 18:11:47.475: WARN/System.err(11083):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
07-31 18:11:47.475: WARN/System.err(11083):     at com.foxbusiness.tv.FOXBusinessActivity.loadCol3(FOXBusinessActivity.java:134)
07-31 18:11:47.475: WARN/System.err(11083):     at com.foxbusiness.tv.FOXBusinessActivity.access$1(FOXBusinessActivity.java:121)
07-31 18:11:47.475: WARN/System.err(11083):     at com.foxbusiness.tv.FOXBusinessActivity$2.onClick(FOXBusinessActivity.java:113)
07-31 18:11:47.475: WARN/System.err(11083):     at android.view.View.performClick(View.java:3110)
07-31 18:11:47.475: WARN/System.err(11083):     at android.view.View$PerformClick.run(View.java:11928)
07-31 18:11:47.485: WARN/System.err(11083):     at android.os.Handler.handleCallback(Handler.java:587)
07-31 18:11:47.485: WARN/System.err(11083):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-31 18:11:47.485: WARN/System.err(11083):     at android.os.Looper.loop(Looper.java:132)
07-31 18:11:47.485: WARN/System.err(11083):     at android.app.ActivityThread.main(ActivityThread.java:4025)
07-31 18:11:47.485: WARN/System.err(11083):     at java.lang.reflect.Method.invokeNative(Native Method)
07-31 18:11:47.485: WARN/System.err(11083):     at java.lang.reflect.Method.invoke(Method.java:491)
07-31 18:11:47.485: WARN/System.err(11083):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
07-31 18:11:47.485: WARN/System.err(11083):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
07-31 18:11:47.485: WARN/System.err(11083):     at dalvik.system.NativeStart.main(Native Method)

07-31 18:09:26.915: ERROR/AndroidRuntime(11024): FATAL EXCEPTION: main
07-31 18:09:26.915: ERROR/AndroidRuntime(11024): java.lang.NullPointerException
07-31 18:09:26.915: ERROR/AndroidRuntime(11024):     at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:112)
07-31 18:09:26.915: ERROR/AndroidRuntime(11024):     at org.json.JSONTokener.nextValue(JSONTokener.java:90)
07-31 18:09:26.915: ERROR/AndroidRuntime(11024):     at org.json.JSONObject.<init>(JSONObject.java:154)
07-31 18:09:26.915: ERROR/AndroidRuntime(11024):     at org.json.JSONObject.<init>(JSONObject.java:171)
07-31 18:09:26.915: ERROR/AndroidRuntime(11024):     at com.example.tv.MyActivity.loadCol3(MyActivity.java:142)
07-31 18:09:26.915: ERROR/AndroidRuntime(11024):     at com.example.tv.MyActivity.access$1(MyActivity.java:121)
07-31 18:09:26.915: ERROR/AndroidRuntime(11024):     at com.example.tv.MyActivity$2.onClick(MyActivity.java:113)
07-31 18:09:26.915: ERROR/AndroidRuntime(11024):     at android.view.View.performClick(View.java:3110)
07-31 18:09:26.915: ERROR/AndroidRuntime(11024):     at android.view.View$PerformClick.run(View.java:11928)
07-31 18:09:26.915: ERROR/AndroidRuntime(11024):     at android.os.Handler.handleCallback(Handler.java:587)
07-31 18:09:26.915: ERROR/AndroidRuntime(11024):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-31 18:09:26.915: ERROR/AndroidRuntime(11024):     at android.os.Looper.loop(Looper.java:132)
07-31 18:09:26.915: ERROR/AndroidRuntime(11024):     at android.app.ActivityThread.main(ActivityThread.java:4025)
07-31 18:09:26.915: ERROR/AndroidRuntime(11024):     at java.lang.reflect.Method.invokeNative(Native Method)
07-31 18:09:26.915: ERROR/AndroidRuntime(11024):     at java.lang.reflect.Method.invoke(Method.java:491)
07-31 18:09:26.915: ERROR/AndroidRuntime(11024):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
07-31 18:09:26.915: ERROR/AndroidRuntime(11024):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
07-31 18:09:26.915: ERROR/AndroidRuntime(11024):     at dalvik.system.NativeStart.main(Native Method)
5
  • 1
    How about posting the stack trace that your code sent to the console immediately after "httpclient.execute failed"? Commented Jul 31, 2011 at 18:05
  • @Ted I see two stack traces in LogCat and I'm not sure which is which so I added them both in an edit. Hopefully that is what you are looking for. Commented Jul 31, 2011 at 18:13
  • There's to much delay. Your program crashes because you run a network operation in the main thread. Commented Jul 31, 2011 at 18:14
  • @Fredrik I'm not sure of your recommendation. Are you recommending that I put this code/function into another class and running it there? Commented Jul 31, 2011 at 18:21
  • Interesting find based on what @Fredrik said: developer.android.com/reference/android/os/… Commented Jul 31, 2011 at 18:39

1 Answer 1

3

You get the NullpointerException because you access the result even when the GET method fails.

As for the NetworkOnMainThreadException, well the problem is exactly that: You do networking on the main thread. Run your GET request in either an AsyncTask or an IntentService.

Sign up to request clarification or add additional context in comments.

1 Comment

I moved the get to an AsyncTask and ran new MyClass.execute("http://someplace.com/json.js"); that fixed that issue. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.