0

我对android开发非常陌生,这是我的第一天。我无法理解我的代码有什么问题,因为我无法理解我收到的错误消息。我只是在寻找 somoene 来了解正在发生的事情。

我正在尝试发送一个带有 url 的 http 请求,我应该得到一个包含完整文本的响应,我尝试将这个正文提取到一个字符串中,并通过 Android 文本发送到语音 api,同时还显示所述页面的网络视图。

这里是:

package com.example.webview;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.Toast;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;

public class MainActivity extends Activity implements TextToSpeech.OnInitListener{

    private WebView mWebview ;
    EditText txtText;
    TextToSpeech tts = new TextToSpeech(this, (OnInitListener) this);
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        mWebview  = new WebView(this);

        mWebview.getSettings().setJavaScriptEnabled(true); // enables javascript

        final Activity activity = this;

        mWebview.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
            }
        });

        mWebview .loadUrl("http://www.androidhive.info/2012/01/android-text-to-speech-tutorial/");
        setContentView(mWebview);
        try {
            speakOut(getText("http://www.androidhive.info/2012/01/android-text-to-speech-tutorial/"));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public void speakOut(String text) {
        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }

    public String getText(String webPage) throws ParseException, IOException{
        HttpResponse response = null;
        try {        
                HttpClient client = new DefaultHttpClient();
                HttpGet request = new HttpGet();
                request.setURI(new URI("http://supersecretwebsite.net:8080/" + "http://www.androidhive.info/2012/01/android-text-to-speech-tutorial/"));
                response = client.execute(request);
            } catch (URISyntaxException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }   
        String responseBody = "No text found on webpage.";
        int responseCode = response.getStatusLine().getStatusCode();
        switch(responseCode) {
        case 200:
        HttpEntity entity = response.getEntity();
            if(entity != null) {
                responseBody = EntityUtils.toString(entity);
            }
        }
        return responseBody;
    }


    @Override
    public void onInit(int status) {
        // TODO Auto-generated method stub

    }

}

错误:

那么有很多。它开始于

(Application)com.example.webview (Tag)Trace (Text)Error opening trace file: No such file or directory (2)

然后一切似乎都只是关闭了。该程序甚至没有运行。当我有一个基本的 webview 并且没有其他任何东西时它工作得很好,所以添加的 Httprequest 或文本到语音可能是这些错误的原因,但老实说,我不知道如何、什么或为什么。

4

1 回答 1

1

您需要在另一个线程上建立任何网络。使用异步任务来实现这一点。然后在 OnPostExecute() 方法中可以显示结果。

您还需要在清单中声明互联网权限。

于 2013-07-05T19:31:08.217 回答