为什么我会得到一个“无法解析符号”'执行''?

时间:2014-03-24 15:20:52

标签: java android apache httpclient http-authentication

我导入了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"});

}

}

2 个答案:

答案 0 :(得分: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;
     }

答案 1 :(得分: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
  }
 }
        ...