2014-03-24 4 views
0

httpclient 패키지를 가져오고 .addHeader 및 .execute를 사용하려고했지만 항상 오류가 발생합니다. '' '심볼을 실행할 수 없습니다' '및'addHeader '' 심볼을 해결할 수 없습니다. 필요한 패키지를 가져 왔지만 .왜 '심볼을 처리 할 수 ​​없습니다'가 실행됩니까?

public class MyActivity extends Activity { 
public TextView quellcode; 
public TextView geschichte; 
private String string1 = "gahwareawbad password gagaw"; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    quellcode = (TextView) findViewById(com.example.MainActivity.R.id.TextView01); 
    geschichte = (TextView) findViewById(com.example.MainActivity.R.id.geschichte); 
    readWebpage(quellcode); 




} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    //getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 
private class DownloadWebPageTask extends AsyncTask<String, Void, String> { 

    //AUTOMATIC LOGIN 


    //END OF LOGIN 

    //READ WEBPAGE 
    @Override 
    protected String doInBackground(String... urls) { 
     String response = ""; 
     downloadPage("http://www.besselgymnasium.de/fileadmin/Vertretung/Vertretungsplan_Schueler/subst_001.htm"); 
     for (String url : urls) { 
      DefaultHttpClient client = new DefaultHttpClient(); 
      HttpGet httpGet = new HttpGet(url); 
      try { 
       HttpResponse execute = client.execute(httpGet); 
       InputStream content = execute.getEntity().getContent(); 

       BufferedReader buffer = new BufferedReader(new InputStreamReader(content)); 
       String s = ""; 
       while ((s = buffer.readLine()) != null) { 
        response += s; 
       } 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
     return response; 

    } 
    //END OF READING 
    private void downloadPage (String url){ 
     try{ 
      URL obj = new URL(url); 
      // Send post request 
      HttpURLConnection con = (HttpURLConnection) obj.openConnection(); 
      con.setDoOutput(true); 
      DataOutputStream wr = new DataOutputStream(con.getOutputStream()); 
      wr.writeBytes("username"+"password"); 
      wr.flush(); 
      wr.close(); 
      InputStream is = con.getInputStream(); 
      InputStreamReader isr = new InputStreamReader(is,"ISO-8859-1"); 
      BufferedReader br = new BufferedReader(isr); 
      for (String line = br.readLine(); line != null ; line = br.readLine()) { 
       // the variable line contains the line of text. You can insert it in your database of file 
      } 
      con.disconnect(); 
     } catch (Exception e){ 
      // TODO error handling 
     } 
    } 
    @Override 
    protected void onPostExecute(String result) { 
     quellcode.setText(result); 

     //CHECK IF CLASS IS CANCELED 
     if(quellcode.getText().toString().contains("eigenverant. Arb.") && quellcode.getText().toString().contains("Pön")){ 
      geschichte.setText("OUTPUT: SUCCESS!"); 
     }else{ 
      geschichte.setText("OUTPUT: FAIL!"); 
     } 

     //END OF CHECK 


    } 
} 


public void readWebpage(View view) { 
    DownloadWebPageTask task = new DownloadWebPageTask(); 
    task.execute(new String[]{"http://www.besselgymnasium.de/fileadmin/Vertretung/Vertretungsplan_Schueler/subst_001.htm"}); 

} 

은}

답변

0

으로 백그라운드 스레드에서 요청을 실행하기 위해 사용 doInBackground :

protected String doInBackground(String... urls) { 
      // call httpclient.execute(request); here 

    HttpUriRequest request = new HttpGet("http://www.besselgymnasium.de/"+ 
       "fileadmin/Vertretung/Vertretungsplan_Schueler/subst_001.htm"); 
    String credentials = "username" + ":" + "password"; 
    String base64EncodedCredentials = 
        Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP); 
    request.addHeader("Authorization", "Basic " + base64EncodedCredentials); 

    HttpClient httpclient = new DefaultHttpClient(); 
    httpclient.execute(request); 
     return null; 
    } 
+0

나는 위의 내 코드를 편집하고 doInBackground 부분을 포함. 코드로 대체해야합니까? – Fynn

+0

@ user2051194 : 예 코드 변경 대답은 –

+0

과 같습니다. 좋아, doInBackground 부분을 코드로 바 꾸었습니다. 이제 오류가 발생합니다 : java :보고되지 않은 예외 java.io.IOException; 이 라인에 이 던져 지거나 잡혀 야합니다. httpclient.execute (request); – Fynn

0

나는 HttpURLConnection 대신 DefaultHttpClient 사용합니다. 여기

예 :

 ... 

protected String doInBackground(String... args) { 
     ... 
     downloadPage("http://www.mysite.com"); 
     ... 
} 

private void downloadPage (String url){ 
try{ 
     URL obj = new URL(url); 
     // Send post request 
     HttpURLConnection con = (HttpURLConnection) obj.openConnection(); 
     con.setDoOutput(true); 
     DataOutputStream wr = new DataOutputStream(con.getOutputStream()); 
     //wr.writeBytes("myurlParameters"); 
     wr.flush(); 
     wr.close(); 
     InputStream is = con.getInputStream(); 
     InputStreamReader isr = new InputStreamReader(is,"ISO-8859-1"); 
     BufferedReader br = new BufferedReader(isr); 
     for (String line = br.readLine(); line != null ; line = br.readLine()) { 
      // the variable line contains the line of text. You can insert it in your database of file 
     } 
     con.disconnect(); 
    } catch (Exception e){ 
    // TODO error handling 
    } 
} 
     ... 
+0

정확히 무엇이 바뀌겠습니까? 그리고 그 코드를 어디에 삽입해야합니까? – Fynn

+0

코드가 –

+0

이상으로 업데이트되었으며 사용자 이름/비밀번호는 어디에서 입력해야합니까? 그게 내 코드를위한 것입니다. – Fynn