2011-07-31 44 views
0
package info.testing; 

import java.io.IOException; 

import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 
import org.jsoup.select.Elements; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.webkit.WebView; 
import android.widget.Toast; 

public class SoupActivity extends Activity { 

private static final String TAG = "SoupActivity"; 
private static final String DATA = null; 
private String data = null; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    if(savedInstanceState != null) 
    { 
     data = savedInstanceState.getString(DATA); 
     showResults(); 
    } 
    else 
    { 
     parsePage(); 
    } 
} 

protected void parsePage(){ 
    Document doc = null; 
    try { 
     doc = Jsoup.connect("http://www.mydata.html").get(); 
     Toast.makeText(this, R.string.success, Toast.LENGTH_SHORT).show(); 
    } catch (IOException e) { 
     Toast.makeText(this, R.string.error, Toast.LENGTH_SHORT).show(); 
    } 

    Elements rows = doc.select("tr[class]"); 

    data = "<table>" + rows.toString() + "</table>"; 
    showResults(); 
} 

protected void showResults(){ 
    WebView web = (WebView)findViewById(R.id.web); 
    web.loadData(data, "text/html", "utf-8"); 
} 

@Override 
public void onSaveInstanceState(Bundle savedInstanceState){ 
    savedInstanceState.putString(DATA, data); 
    super.onSaveInstanceState(savedInstanceState); 
} 

@Override 
public void onRestoreInstanceState(Bundle savedInstanceState){ 
    if(savedInstanceState != null) 
    { 
     data = savedInstanceState.getString(DATA); 
    } 
    super.onRestoreInstanceState(savedInstanceState); 
} 
} 

的Flash/Flex开发者在这里开始获得在Android开发关闭,我必须承认,我喜欢它,到目前为止,但很明显,花费很长的时间来解决的事情,为什么会发生他们的方式。的Android - 应用力在没有互联网连接

所以我遇到的问题是我的应用程序崩溃没有互联网连接 - 应用程序(process.testing)意外停止。这只发生在没有互联网连接的情况下,如果有的话可以完美地工作。我的代码访问Internet的唯一部分是在try catch块中,任何人都可以看到我在做什么错误,或者当没有可用的Internet连接时如何处理错误?

回答

1

当你有没有互联网连接,doc可能是零,你会得到NullPointerException,因为你不检查这种情况:

Document doc = null; 
try { 
    // connect throws an exception, doc still null 
    doc = Jsoup.connect("http://www.mydata.html").get(); 
    Toast.makeText(this, R.string.success, Toast.LENGTH_SHORT).show(); 
} catch (IOException e) { 
    Toast.makeText(this, R.string.error, Toast.LENGTH_SHORT).show(); 
} 

// dereferencing null (doc) throws NullPointerException 
Elements rows = doc.select("tr[class]"); 
+0

是的,当然,有点小学生的错误,如果我更熟悉调试器,我会抓住那一个,谢谢。 – Neil

+0

那么下一次呢... – MByD

7

您可以使用此功能,看是否有连接可用:

/** 
* Check the network state 
* @param context context of application 
* @return true if the phone is connected 
*/ 
public static boolean isConnected(Context context) { 
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo netInfo = cm.getActiveNetworkInfo(); 
    if (netInfo != null && netInfo.isConnected()) { 
     return true; 
    } 
    return false; 
} 
+0

感谢这个信息它将是非常有用的,我已经接受MByD的答案,因为它直接关系到我的错误。 – Neil

+1

是的,但我的测试比MByD方法更具反应性 – Guillaume