android 网络连接报500,Android HttpUrlConnection returns 500

I'm trying to access open weather api using android. I used two methods, one for creating url and one for getting http response.

Creating url

public static String API_LINK="http://samples.openweathermap.org/data/2.5/weather";

public static String apiRequest(String lat, String lng){

StringBuilder sb=new StringBuilder();

Uri builtUri = Uri.parse(API_LINK)

.buildUpon()

.appendQueryParameter("lat", lat)

.appendQueryParameter("lon", lng)

.appendQueryParameter("appid", API_KEY)

.build();

sb.append(builtUri.toString());

return sb.toString();

}

Http request

public String getHTTPData(String urlString){

try {

URL url=new URL(urlString);

HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();

httpURLConnection.setRequestMethod("POST");

httpURLConnection.setDoInput(true);

httpURLConnection.setDoOutput(true);

Log.d("Helper", httpURLConnection.getResponseCode()+"");

if(httpURLConnection.getResponseCode()==200){//ok=200

BufferedReader r=new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(), "UTF-8"));

StringBuilder sb=new StringBuilder();

String line;

while ((line=r.readLine())!=null){

sb.append(line);

stream=sb.toString();

httpURLConnection.disconnect();

}

}

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return stream;

}

And i used a asyncTask to call this method in MainActivity

new GetWeather().execute(apiRequest("65.9667","-18.5333"));

But every time I make a request I get 500 http response code. I tried to navigate created url using browser, It's working. It not worked in my emulator.

How can i solve this. Thank you

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值