连接到API时出现异常

时间:2015-10-07 19:24:41

标签: android json http exception

    protected JSONObject doInBackground(Object... params) {
        int responsecode = -1;
        JSONObject jsonResponse = null;
        try{
            URL HeadingUrl = new URL("http://blog.teamtreehouse.com/api/get_recent_summary/?count=" + NUMBER_OF_POSTS); // Create a URL object
            HttpURLConnection connection = (HttpURLConnection) HeadingUrl.openConnection();   //we would make a connection object so as to connect to the url
            // above line may throw Malformed exception
            connection.connect();   // connection to the required url & also may throw IO exception

我不知道为什么连接对象在连接到提到的url时抛出io异常,并且也无法追查它的原因。 请帮忙

这是log cat msg

1525-1525/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.google.android.ears, PID: 1525
java.lang.AssertionError: Unexpected parameter type list size: 0
        at com.android.dex.Dex.parameterTypeIndicesFromMethodIndex(Dex.java:426)
        at java.lang.reflect.ArtMethod.equalConstructorParameters(ArtMethod.java:106)
        at java.lang.Class.getDeclaredConstructorInternal(Class.java:553)
        at java.lang.Class.getConstructor(Class.java:529)
        at java.lang.Class.getDeclaredConstructor(Class.java:510)
        at java.lang.Class.newInstance(Class.java:1595)
        at android.app.ActivityThread.installProvider(ActivityThread.java:4987)
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:4594)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4534)
        at android.app.ActivityThread.access$1500(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        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:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

OnCreate方法

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_list);
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);

    if (isNetworkEnabled()) {
        mProgressBar.setVisibility(View.VISIBLE);
        GetTitle gettitle = new GetTitle();
        gettitle.execute();
    }
    else {
        Toast.makeText(this,"Check your Network Connection",Toast.LENGTH_LONG).show();
    }
}

private boolean isNetworkEnabled() {
    ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo =manager.getActiveNetworkInfo();
    boolean isAvaialble = false;

    //checking first if the network is not null and then network is connected to web or not
    if(networkInfo!=null && networkInfo.isConnected())
    {
        isAvaialble = true;
    }
    return isAvaialble;
}

1 个答案:

答案 0 :(得分:0)

你不应该在没有参数的情况下调用execute()方法,那就是错误。

将onCreate()更改为以下内容:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_list);
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);

    if (isNetworkEnabled()) {
        mProgressBar.setVisibility(View.VISIBLE);
        GetTitle gettitle = new GetTitle();
        gettitle.execute(new Object[0]);
    }
    else {
        Toast.makeText(this,"Check your Network Connection",Toast.LENGTH_LONG).show();
    }
}