Android AsyncTask提供了奇怪的行为
我正在尝试调用API以获取YouTube视频列表,如下所示:Android AsyncTask提供了奇怪的行为,android,android-asynctask,logcat,android-logcat,Android,Android Asynctask,Logcat,Android Logcat,我正在尝试调用API以获取YouTube视频列表,如下所示: @Override protected String doInBackground(String... theParams) { String myUrl = theParams[0]; final String key = "my_key"; final String channelId = "UCoLEarNS6E-Kbzoya_p7k2Q";
@Override
protected String doInBackground(String... theParams)
{
String myUrl = theParams[0];
final String key = "my_key";
final String channelId = "UCoLEarNS6E-Kbzoya_p7k2Q";
String charset = "UTF-8";
String response = null;
try
{
String query = String.format("key=%s&channelId=%s&part=snippet,id&order=date&maxResults=20",
URLEncoder.encode( key, charset ) ,
URLEncoder.encode( channelId, charset )
);
final URL url = new URL( myUrl + "?" + query );
final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.connect();
final InputStream is = conn.getInputStream();
final byte[] buffer = new byte[8196];
int readCount;
final StringBuilder builder = new StringBuilder();
while ((readCount = is.read(buffer)) > -1)
{
builder.append(new String(buffer, 0, readCount));
}
response = builder.toString();
}
catch (IOException e)
{
sendEmail ( "MyQuestionsActivity Network Error" , "Error: " + e.getMessage() );
}
return response;
}
但它总是出错。如果我将url粘贴到浏览器中,它会返回一些JSON。但在本例中,我创建的sendEmail实用程序正在从e.getMessag()向我发送此错误:
有人知道什么地方不对劲吗?此外,我的catLog出现了一些问题,因此无法看到异常情况。您确定需要conn.setRequestMethod(“POST”)?我认为这是得到,因为你这是什么浏览器时,你把它。以及conn.setDoOutput(真);和设置POST的方法相同。很好。现在就试试看。不,那也不行。我应该如何处理setDoOutput方法?我对此有点困惑。另外,我认为如果您将doOutput设置为“true”,默认情况下会将其作为POST,那么您应该conn.setDoInput(true);并删除dooutput…在建议的更改后仍然会收到相同的错误
Error: https://www.googleapis.com/youtube/v3/search?key=my_key&channelId=UCoLEarNS6E-Kbzoya_p7k2Q&part=snippet,id&order=date&maxResults=20