gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 15:09:57 24 4
gpt4 key购买 nike

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

我成功登录的网址是:

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

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

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

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

最佳答案

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

                 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;

}

关于Android:获取用户代理对象中设置的json对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17694749/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com