我是新来的android和我想连接到json文件,但是当我尝试此代码,但它不连接,当我在我的手机上运行它时,我想工作,它会得到我在logcat的nooo根据我说,当它有问题连接到服务器,谢谢使用android连接到服务器
public class LastFMHelper {
public static final String LastFMMetroTrackChartUrl=
"http://ws.audioscrobbler.com/2.0/?method=geo.getmetrotrackchart&country=united+states&metro=denver&format=json&api_key=bc8ae5008414312b31e8c23f684d67cc";
private static final int HTTP_STATUS_OK = 200;
private static byte[] buff = new byte[1024];
private static final String logTag="LastFMHelper";
public static class ApiException extends Exception
{
private static final long serialVersionUID= 1L;
public ApiException (String msg)
{
super(msg);
}
public ApiException(String msg, Throwable thr)
{
super(msg, thr);
}
}
protected static synchronized String downlaodFromServer (String ...params) throws ApiException {
String retval= null;
String metro = params[0];
String url= LastFMMetroTrackChartUrl + "&metro" +metro;
Log.d(logTag,"Fetching"+url);
HttpClient client= new DefaultHttpClient();
HttpGet request = new HttpGet(url);
try{
HttpResponse responce = client.execute(request);
StatusLine status = responce.getStatusLine();
if(status.getStatusCode()!=HTTP_STATUS_OK)
{
throw new ApiException("Invalid responce from last.fm"+status.toString());
}
HttpEntity entity = responce.getEntity();
InputStream ist = entity.getContent();
ByteArrayOutputStream content = new ByteArrayOutputStream();
int readcount =0;
while((readcount= ist.read(buff)) != -1)
{
content.write(buff,0,readcount);
}
retval= new String (content.toByteArray());
}catch(Exception e){
Log.d(logTag,"noo!!");
throw new ApiException("problem connecting to server " + e.getMessage(),e);
}
return retval;
}
}
ApiException打印到日志是什么?如果什么都没有,请尝试记录异常并发布日志。例如,而不是'Log.d(logTag,“noo !!”);'尝试'Log.d(logTag,e.getMessage());' – katzenhut
我这样做05-25 12:14:37.882 :D/LastFMHelper(22953):权限被拒绝(缺少INTERNET权限?) – amirhtk