3

我正在向 Facebook 图形 API 发出 HTTP-get 请求。

在大约 1/5 次中,我的代码永远不会到达Log.i("debug", "resp");. 没有抛出异常。不应该吗?还是默认情况下它只是一个很长的超时?

如果我添加自定义超时(见下文),我会抛出异常。但即使我的代码包含在 try+catch 语句中,我的应用程序也会崩溃(就像任何未处理的异常一样),而不是让我处理onPostExecute(). 为什么我不使用该方法?

protected Map<String, Integer> doInBackground(Void... params) {

    Map<String, Integer> result = new HashMap<String, Integer>();

    try {

        HttpGet get = new HttpGet("https://graph.facebook.com/....etc");

        //final HttpParams httpParams = httpclient.getParams();
        //HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
        //HttpConnectionParams.setSoTimeout(httpParams, 5000);

        HttpResponse response = httpclient.execute(get);
        Log.i("debug", "resp");  

        HttpEntity resEntityGet = response.getEntity();

        //do stuff with resEntityGet           

        return result;            

    } catch (Exception e) { 
        Toast.makeText(mainActivity, "Error: " + ex.getMessage(), Toast.LENGTH_LONG).show();
        return null;
    }
}

protected void onPostExecute(Map<String, Integer> result) {

    if(result != null){
        //use the result data
    } else {
        //exception occured
    }
}
4

5 回答 5

1

如果您将其发布到 UI 线程,则可以显示Toast来自该方法的方法。doInBackground有几种方法可以做到这一点,但这里有一种方法:

mainActivity.runOnUiThread( new Runnable() {

    @Override
    public void run() {
        Toast.makeText(mainActivity, "Error: " + ex.getMessage(), Toast.LENGTH_LONG).show();
    }
} );

你需要使Exception变量final

于 2012-12-07T20:53:04.733 回答
0
catch (Exception ex) {
       // execep is here string variable u declare in globally
       execep=ex.getMessage();
      runOnUiThread( new Runnable() {
   @Override
public void run() {
  Toast.makeText(  getApplicationContext(), "Error: " +execep, Toast.LENGTH_LONG).show();
                                        }} );

                               }//catch
于 2012-12-08T06:28:11.640 回答
0

您的代码是正确的,但它并不清晰和安全。如果未设置超时,则将其设置为默认值 0。超时值为零被解释为无限超时。在这种情况下,当响应数据因任何原因出现问题时,您的客户端要么被中断,要么什么都不知道。在情况 2 - 更坏的情况下 - 客户端不会抛出任何异常,您的程序将永远存在。解决此问题,您应该在获取或发布所有内容到服务器时设置超时。它是安全的。我不完全知道客户永远活着的原因,没有任何例外。我认为这是服务器问题或网络问题。希望能帮到你。

于 2012-12-12T10:25:43.070 回答
0

当我以这种方式处理异常时,我也遇到了同样的问题

public class ServerCheckingActvity extends Activity{
    ProgressDialog  progress;
    static String constant="";
    Map<String, Integer> result;
    @Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     new MyAsynchTask().execute();
}

private  class MyAsynchTask extends AsyncTask<Void,Void,String>{
    @Override
    protected void onPreExecute(){
           progress=new ProgressDialog(ServerCheckingActvity.this);
           // progress.setTitle(" DATA RETRIVEING");
            progress.setMessage("please wait........");
            progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progress.setCancelable(true);
            progress.show();
            super.onPreExecute();
    }

    @Override
    protected String doInBackground(Void... params) {
        String view="";

        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();

            HttpGet get = new HttpGet("www.facebook .com/...........");

            //final HttpParams httpParams = httpclient.getParams();
            //HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
            //HttpConnectionParams.setSoTimeout(httpParams, 5000);

            HttpResponse response =httpClient.execute(get);
            Log.i("debug", "resp");  

            HttpEntity resEntityGet = response.getEntity();

            //do stuff with resEntityGet           

           // assigen ur value to result map object here;    
            result = new HashMap<String, Integer>();


        } catch (Exception e) { 
            constant="Exception";
            //Toast.makeText(mainActivity, "Error: " + ex.getMessage(), Toast.LENGTH_LONG).show();
            //return null;
        }
        return view="from doing background";
    }
    @Override
    protected void onPostExecute(String view ){
        if(constant=="Exception"){
            constant="";
            Toast.makeText(getApplicationContext(), "server problem", Toast.LENGTH_LONG).show();

        }else if(result != null){
            //use the result data
        } else {
            //exception occured
        }
    }

}
于 2012-12-08T06:06:55.233 回答
0

Figured it out myself. Turns out that you can't create toasts in doInBackground(). Hope this helps someone else.

于 2012-12-05T18:59:01.183 回答