Android HttpUrlConnection返回500

时间:2017-09-05 09:40:48

标签: android httpurlconnection openweathermap

我试图使用android访问开放天气api。我使用了两种方法,一种用于创建url,另一种用于获取http响应。

创建网址

class Test(object):
    def display(self, x):
        self.x = x
        print self.x

class MyTest(Test):
    def someother(self):
        print "i am a different method"
    def display(self, x):
        self.x = x
        print "Getting called form MyTest"



a = MyTest()
z = super(type(a), a).display(10)
print z

10
None

Http请求

 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();
    }

我使用asyncTask在MainActivity中调用此方法

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;

    }

但每次我提出请求时,我都会得到500个http响应代码。我尝试使用浏览器导航创建的URL,它正在工作。它在我的模拟器中无效。

我该如何解决这个问题。谢谢

1 个答案:

答案 0 :(得分:0)

500 Internal Server Error

  

服务器遇到阻止它的意外情况   满足要求。

可能是 SERVER 错误。此错误只能通过修复Web服务器来解决。