Java Android http请求出现了可怕的错误
我对android开发非常陌生,这是我的第一天。我在理解代码的错误方面遇到了问题,因为我无法理解收到的错误消息。我只是想让somoene对正在发生的事情有所了解 我正试图发送一个带有url的http请求,我应该得到一个充满文本的响应,我试图将这个文本提取到一个字符串中,并通过Android文本到语音api发送它,同时显示所述页面的webview 这是:Java Android http请求出现了可怕的错误,java,android,Java,Android,我对android开发非常陌生,这是我的第一天。我在理解代码的错误方面遇到了问题,因为我无法理解收到的错误消息。我只是想让somoene对正在发生的事情有所了解 我正试图发送一个带有url的http请求,我应该得到一个充满文本的响应,我试图将这个文本提取到一个字符串中,并通过Android文本到语音api发送它,同时显示所述页面的webview 这是: package com.example.webview; import java.io.IOException; import java.ne
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或文本到语音可能是这些错误的原因,但我真的不知道如何、什么或为什么。你需要在另一个线程上进行任何联网。使用异步任务来实现这一点。然后在OnPostExecute()方法中可以显示结果
您还需要在清单中声明internet权限。
新URI(“http://supersecretwebsite.net:8080/" + "http://www.androidhive.info/2012/01/android-text-to-speech-tutorial/)
。。。这不是有效的URL。您发布的消息可能与失败无关,或者至少与失败没有直接关系。关于Android,首先要了解的是,有很多错误和警告消息,其中一些实际上是例行的,你需要查看完整的日志以找到实际问题的线索——也就是说,如果存在超出Brian上述识别范围的问题。