我是Android开发的新手,这是我的第一天。我在理解我的代码有什么问题时遇到了问题,因为我无法理解我得到的错误消息。我只是在寻找索蒙来揭示正在发生的事情。
我正在尝试使用url发送一个http请求,我应该得到一个带有文本的主体的响应,我尝试将此主体提取到一个String上,并通过Android文本发送到语音api同时还显示所述页面的webview。
这是:
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或文本到语音可能是导致这些错误的原因,但老实说我不知道如何,什么或为什么。
答案 0 :(得分:1)
您需要在另一个线程上拥有任何网络。使用异步任务来实现此目的。然后在OnPostExecute()方法中,您可以显示结果。
您还需要在清单中声明互联网权限。