Android:获取用户代理对象中设置的json对象

时间:2013-07-17 08:25:32

标签: android json webview user-agent google-account

在我的应用程序中,我使用谷歌帐户登录登录到成功登录后由我们的服务器端代码验证的应用程序,并在完成后将控件返回到我们的网站自定义URL。

我成功登录的网址是:

07-17 13:45:07.123: E/start(7208): https://www.mywebsite.com/auth/google/callback?code=4/tCw3uOWulXr0RO1ZG0C1hanEWNDc.IgNbwOdJ7uEcXE-sT2ZLcbSfjWjXfwI

我能够在webview上看到json对象,它可能会回显到它上面。猜测不是网络开发者。附上屏幕截图。

服务器根据我的应用程序明确声明的用户代理单独响应,以便我们的自定义服务器会相应地响应网站和我们的应用程序:

WebView login=(WebView)findViewById(R.id.loginWebview);

    login.getSettings().setUserAgentString("user_agent_app");

尝试将上面提到的最后一个url作为来自服务器的响应,但它返回null虽然我可以在webview中看到结果。

public static String getResponse(String url){
        String downloadedData = null;
        Log.e("getResponse", url);
        try {
            URL downloadURL = new URL(url);
            InputStream inputStream = (InputStream) downloadURL.getContent();
            if (null != inputStream) {
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                byte[] buffer = new byte[512];
                int readCounter = inputStream.read(buffer);
                while (readCounter != -1) {
                    byteArrayOutputStream.write(buffer, 0, readCounter);
                    readCounter = inputStream.read(buffer);
                }
                downloadedData = new String(
                        byteArrayOutputStream.toByteArray());
                /*if (null != downloadedData && !"".equals(downloadedData)) {
                    downloadedJson = new JSONObject(downloadedData);
                }*/
            }else{
                Log.e("getResponse", "Response is null");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return downloadedData;
    }

如果有人可以帮助我检索json对象,那将是值得注意的。

enter image description here

1 个答案:

答案 0 :(得分:0)

我不确定,但它对你有所帮助。试试这个:

                 JSONObject jObject;
                 public JSONObject getJSONFromUrl(String url) { 
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost); 
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();           

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace(); 
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close(); 
            json = sb.toString(); 
                   } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object 
        try {
           jObject = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObject;

    }