无法从HttpURLConeection获得响应

时间:2018-09-09 15:43:11

标签: android rest

我是android新手。我想从本地主机调用API,但未收到任何响应。这是我的代码

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shopify_list);

InputStream inputStream = null;
HttpURLConnection urlConnection = null;
URL url = null;

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shopify_list);

InputStream inputStream = null;
HttpURLConnection urlConnection = null;
URL url = null;

try {

    url = new URL("http://localhost/practice/json.php");

    urlConnection = (HttpURLConnection) url.openConnection();

    inputStream = urlConnection.getInputStream();

    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

    String line;
    StringBuffer response = new StringBuffer();

    while ((line = bufferedReader.readLine()) != null ){
        response.append(line);
    }

    Toast.makeText(this, response.toString(), Toast.LENGTH_LONG).show();

} catch (Exception e){

} finally {

    if( inputStream != null ){

        try {
            inputStream.close();
        } catch (IOException ignored){

        }
    }
    if( urlConnection != null ){
        urlConnection.disconnect();
    }
}

我无法在屏幕上看到Toast。我不知道我在做什么错。请帮我。

谢谢。

0 个答案:

没有答案