2014-02-19 161 views
0

HTTP请求的错误代码,这是我的http请求的代码,以显示在TextView中从一个文本文件中的文本如何更改android系统

private TextView txtdata; 
final String textSource = "http://orthodoxprayers.yolasite.com/resources/saint_elie_sinelfil.txt"; 



txtdata = (TextView)findViewById(R.id.txtdata); 
    new MyTask().execute(); 
    URL textUrl; 

     try { 
     textUrl = new URL(textSource); 

     BufferedReader bufferReader 
     = new BufferedReader(new InputStreamReader(textUrl.openStream())); 

     String StringBuffer; 
     String stringText = ""; 
     while ((StringBuffer = bufferReader.readLine()) != null) { 
     stringText += StringBuffer; 
     } 
     bufferReader.close(); 

     txtdata.setText(stringText); 
     } catch (MalformedURLException e) { 
     e.printStackTrace(); 
     txtdata.setText(e.toString()); 
     } catch (IOException e) { 
     e.printStackTrace(); 
     txtdata.setText(e.toString()); 
     } 

    } 

    private class MyTask extends AsyncTask<Void, Void, Void>{ 

    String textResult; 

    @Override 
    protected Void doInBackground(Void... params) { 

     URL textUrl; 

     try { 
     textUrl = new URL(textSource); 

     BufferedReader bufferReader 
      = new BufferedReader(new InputStreamReader(textUrl.openStream())); 

     String StringBuffer; 
     String stringText = ""; 
     while ((StringBuffer = bufferReader.readLine()) != null) { 
      stringText += StringBuffer; 
     } 
     bufferReader.close(); 

     textResult = stringText; 
     } catch (MalformedURLException e) { 
     e.printStackTrace(); 
     textResult = e.toString(); 
     } catch (IOException e) { 
     e.printStackTrace(); 
     textResult = e.toString(); 
     } 

    return null; 

    } 

    @Override 
    protected void onPostExecute(Void result) { 

    txtdata.setText(Html.fromHtml(textResult)); 
    super.onPostExecute(result); 
    } 

所以当问题发生的这一请求,我不想让http错误,我只想得到一个吐司告诉“下载文本的问题”

回答

0

你应该显示错误日志或其他消息,指出错误发生的地方,这太多的代码,并不知道错误的地方

0

你可以试试这个:

为此在asyntask doinbackground()

HttpParams httpParams = new BasicHttpParams(); 
     //int some_reasonable_timeout = (int) (30 * DateUtils.SECOND_IN_MILLIS); 
     int some_reasonable_timeout = 10; 
     HttpConnectionParams.setConnectionTimeout(httpParams, some_reasonable_timeout); 
     HttpConnectionParams.setSoTimeout(httpParams, some_reasonable_timeout); 
     //DefaultHttpClient client = new DefaultHttpClient(); 

     // get the response 
     HttpResponse response = null; 
     try { 
      response = client.execute(request); 
     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

,并为此在postexecute

 StatusLine status = response.getStatusLine(); 
     Log.d("tag","status is"+status.toString()); 
      if (status.getStatusCode() == HttpStatus.SC_OK) 
      { 
      //download file (download file in another asyntask or you can use thread here but thread will not notify you ,though asyntask does.) 
      } 
      else 
      { 
       //show toast 
      } 
+0

对不起,先生,但我应该在哪里放置或使用该代码 – user3328733

+0

编辑我answer..check它 –