Android App和Http请求

时间:2013-11-19 21:42:13

标签: android google-app-engine http

我目前正在尝试从Android应用程序向google-app-engine发送http请求,服务器应该接收此请求,该服务器将使用URL中传递的参数将新项目添加到数据存储区。

我写了这段代码:

private class AsyncConnection extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... params) {
        try {
            // creating the url
            URL url = new URL(params[0]);

            // opening the connection
            URLConnection connection;
            connection = url.openConnection();

            // get data about the connection
            HttpURLConnection httpConnection = (HttpURLConnection) connection;
            int responseCode = httpConnection.getResponseCode();

            // connection was properly established
            if (responseCode == HttpURLConnection.HTTP_OK) {
                InputStream input = httpConnection.getInputStream();
                return input.toString();
            } else {
                Log.d("CONNECTION", "connection not HTTP_OK");
            }
        } catch (MalformedURLException e) {
            Log.d("SMARTGAN", "MalformedURLException" ,e);
        } catch (IOException e) {
            Log.d("SMARTGAN", "IOException" ,e);
        } catch (Exception e) {
            Log.d("SMARTGAN", "Exception" ,e);
        } finally { }

        return null;
    }
}

但是当我尝试执行它时,我在数据存储区中看不到任何新项目。 URL本身和服务器上的代码都很好,当我尝试并使用它发送URL工作。我没有在日志中看到任何“连接不正常”消息的错误消息。

1 个答案:

答案 0 :(得分:0)