如何从网址获取内容

时间:2013-06-25 15:44:19

标签: android http url get

我尝试通过Timer从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中错误:-( 请帮帮我,给我举例!

0 个答案:

没有答案