1

我尝试通过计时器从 url 获取内容,此代码适用于 android 2.3.6,但不适用于 android 4.2.2

代码:

这个计时器代码:

_mp3GetTrackHandler = new Handler();
_mp3GetTrackTask = new TimerTask(){
@Override
public void run() {
_mp3GetTrackHandler.post(new Runnable(){
@Override
public void run() {
// --- Get Title Tracks ---
getResponse();
// --- End Title Tracks ---
}
});
}
};
_mp3GetTrackTimer = new Timer();
_mp3GetTrackTimer.schedule(_mp3GetTrackTask, 10000, 10000);

获取内容的这段代码:

public void getResponse()
{
HttpClient client = new DefaultHttpClient();    
String query = "http://sspu.sumy.ua/load/app_api.php?artist";

try
{
URL url = new URL(query);
URI uri = new URI(url.getProtocol(), url.getHost(),url.getPath(), url.getQuery(),null);
HttpGet request = new HttpGet(uri);
request.setHeader("User-Agent", "Megasoft Europa Plus ("+ manufacturer +"-"+ model +"; Android)");
HttpResponse response = client.execute(request);
result=Userrequest(response);

}
catch(Exception ex) {
}

}

public String Userrequest(HttpResponse response)
{
try     
{
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null)
{
str.append(line + "\n");
}
in.close();
result = str.toString();
_mp3LCDLabel.setText(result);         
}
catch(Exception ex)
{
//responsetxt.setText(ex.getMessage());
}
return result;
} 

在 Android 4.2.2 中出现错误 :-( 请帮帮我,给我一个工作示例!

4

0 回答 0