- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想解析我从链接下方获取的 JSON
你可以看到我在浏览器中得到了正确的响应,但是我在从我的代码中得到响应时遇到了以下错误:
Logcat 响应:
01-23 11:54:57.415: E/AndroidRuntime(2190): FATAL EXCEPTION: AsyncTask #1
01-23 11:54:57.415: E/AndroidRuntime(2190): java.lang.RuntimeException: An error occured while executing doInBackground()
01-23 11:54:57.415: E/AndroidRuntime(2190): at android.os.AsyncTask$3.done(AsyncTask.java:200)
01-23 11:54:57.415: E/AndroidRuntime(2190): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
01-23 11:54:57.415: E/AndroidRuntime(2190): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
01-23 11:54:57.415: E/AndroidRuntime(2190): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
01-23 11:54:57.415: E/AndroidRuntime(2190): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
01-23 11:54:57.415: E/AndroidRuntime(2190): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
01-23 11:54:57.415: E/AndroidRuntime(2190): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
01-23 11:54:57.415: E/AndroidRuntime(2190): at java.lang.Thread.run(Thread.java:1019)
01-23 11:54:57.415: E/AndroidRuntime(2190): Caused by: java.lang.IllegalArgumentException: Illegal character in query at index 116: http://maps.googleapis.com/maps/api/directions/json?origin=Chicago,IL&destination=Los+Angeles,CA&waypoints=Joplin,MO|Oklahoma+City,OK&sensor=false
01-23 11:54:57.415: E/AndroidRuntime(2190): at java.net.URI.create(URI.java:776)
01-23 11:54:57.415: E/AndroidRuntime(2190): at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:75)
01-23 11:54:57.415: E/AndroidRuntime(2190): at com.example.gisapp.FirstActivity.connect(FirstActivity.java:155)
01-23 11:54:57.415: E/AndroidRuntime(2190): at com.example.gisapp.FirstActivity$AsynTaskForGettingData.doInBackground(FirstActivity.java:109)
01-23 11:54:57.415: E/AndroidRuntime(2190): at com.example.gisapp.FirstActivity$AsynTaskForGettingData.doInBackground(FirstActivity.java:1)
01-23 11:54:57.415: E/AndroidRuntime(2190): at android.os.AsyncTask$2.call(AsyncTask.java:185)
01-23 11:54:57.415: E/AndroidRuntime(2190): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
01-23 11:54:57.415: E/AndroidRuntime(2190): ... 4 more
01-23 11:55:00.045: E/WindowManager(2190): Activity com.example.gisapp.FirstActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405234a0 that was originally added here
01-23 11:55:00.045: E/WindowManager(2190): android.view.WindowLeaked: Activity com.example.gisapp.FirstActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405234a0 that was originally added here
01-23 11:55:00.045: E/WindowManager(2190): at android.view.ViewRoot.<init>(ViewRoot.java:258)
01-23 11:55:00.045: E/WindowManager(2190): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
01-23 11:55:00.045: E/WindowManager(2190): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
01-23 11:55:00.045: E/WindowManager(2190): at android.view.Window$LocalWindowManager.addView(Window.java:424)
01-23 11:55:00.045: E/WindowManager(2190): at android.app.Dialog.show(Dialog.java:241)
01-23 11:55:00.045: E/WindowManager(2190): at com.example.gisapp.FirstActivity$AsynTaskForGettingData.onPreExecute(FirstActivity.java:95)
01-23 11:55:00.045: E/WindowManager(2190): at android.os.AsyncTask.execute(AsyncTask.java:391)
01-23 11:55:00.045: E/WindowManager(2190): at com.example.gisapp.FirstActivity.onCreate(FirstActivity.java:49)
01-23 11:55:00.045: E/WindowManager(2190): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-23 11:55:00.045: E/WindowManager(2190): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-23 11:55:00.045: E/WindowManager(2190): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-23 11:55:00.045: E/WindowManager(2190): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-23 11:55:00.045: E/WindowManager(2190): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-23 11:55:00.045: E/WindowManager(2190): at android.os.Handler.dispatchMessage(Handler.java:99)
01-23 11:55:00.045: E/WindowManager(2190): at android.os.Looper.loop(Looper.java:130)
01-23 11:55:00.045: E/WindowManager(2190): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-23 11:55:00.045: E/WindowManager(2190): at java.lang.reflect.Method.invokeNative(Native Method)
01-23 11:55:00.045: E/WindowManager(2190): at java.lang.reflect.Method.invoke(Method.java:507)
01-23 11:55:00.045: E/WindowManager(2190): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-23 11:55:00.045: E/WindowManager(2190): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-23 11:55:00.045: E/WindowManager(2190): at dalvik.system.NativeStart.main(Native Method)
这是我的 FirstActivity 代码:
package com.example.gisapp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
public class FirstActivity extends Activity {
// Tag Variable to Easily Detect your Activity inside LogCat While you Are
// Placing your Log
static String TAG = "FirstActivity";
// boolean to Mantain the State of Internet Working or Not Default Set to
// true
boolean net = true;
// Progreess Dialogue to Show User when AsyncTask is Doing Work in
// BackGround
ProgressDialog pd;
// String Variable to Store the Data of your Json Response as String
static String result;
String URL = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.firstactivity);
// Execute the AsyncTask
new AsynTaskForGettingData().execute();
}
// For Converting the Given String in the Formate the Website Link otherwise
// you will get illegal Formate Exception cause this String will not be in a
// formate of URL
public static String convertURL(String str) {
String url = null;
try {
url = new String(str.trim().replace(" ", "%20").replace("&", "%26")
.replace(",", "%2c").replace("(", "%28")
.replace(")", "%29").replace("!", "%21")
.replace("=", "%3D").replace("<", "%3C")
.replace(">", "%3E").replace("#", "%23")
.replace("$", "%24").replace("'", "%27")
.replace("*", "%2A").replace("-", "%2D")
.replace(".", "%2E").replace("/", "%2F")
.replace(":", "%3A").replace(";", "%3B")
.replace("?", "%3F").replace("@", "%40")
.replace("[", "%5B").replace("\\", "%5C")
.replace("]", "%5D").replace("_", "%5F")
.replace("`", "%60").replace("{", "%7B")
.replace("|", "%7C").replace("}", "%7D"));
} catch (Exception e) {
e.printStackTrace();
}
return url;
}
// Asynctask To get Data from Server
public class AsynTaskForGettingData extends AsyncTask<Void, Void, Void> {
// After we have Execute AsynTaskForGettingData the First Method That
// Will Called up is onPreExecute()
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
// Show Progress Dialogue to Tell User to Wait For A While untill we
// finishes to get the Data From Server and Parse Json
pd = new ProgressDialog(FirstActivity.this);
pd.setTitle("GISApp");
pd.setMessage("Please Wait..");
pd.show();
}
// After Executing onPreExecute() the Method Will Do work inside
// doInBackground()
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
// URL = convertURL(URL);
StringBuffer sb = new StringBuffer();
sb.append("http://maps.googleapis.com/maps/api/directions/json?origin=Chicago,IL&destination=Los+Angeles,CA&waypoints=Joplin,MO|Oklahoma+City,OK&sensor=false");
URL = sb.toString();
if (connect(URL)) {
try {
JSONObject j_obj = new JSONObject(result);
// msg = j_obj.getString("MESSAGE");
// Log.i(TAG, "Value of MESSAGE" + msg);
/*
* if (msg.equalsIgnoreCase("Password sent")) {
* Toast.makeText( LoginActivity.this,
* "Your Username & Password Has Been Sent to your Email Address"
* , Toast.LENGTH_LONG) .show(); } else if (msg
* .equalsIgnoreCase("fail")) { Toast.makeText(
* LoginActivity.this, "Please Enter Valid Email Address",
* Toast.LENGTH_LONG) .show(); }
*/
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
// msg = "error";
}
return null;
}
// After Finishing Work doInBackground() the Method Will execute its
// onPostExecute method
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
pd.dismiss();
}
}
// Method That Will Check Whether the Internet is Working or Not and If
// Working it will Set the Response inside result Variable.
@SuppressWarnings("finally")
public static boolean connect(String url) {
boolean flag = false;
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget = new HttpGet(url);
Log.e("url", url);
// Execute the request
HttpResponse response;
try {
// https://api.vkontakte.ru/method/audio.search?uid=163398985&q=akoncount=100&access_token=2a4db0e223f0f5ab23f0f5ab5f23da5680223f023f1f5a3c696b018be9b17b9
response = httpclient.execute(httpget);
response.addHeader("Accept-Language", "zh-CN");
// Examine the response status
Log.i(TAG, response.getStatusLine().toString() + "\n" + response);
// Get hold of the response entity
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
// to worry about connection release
if (entity != null) {
// A Simple JSON Response Read
InputStream instream = entity.getContent();
Log.d(TAG, "---- instream --- " + instream);
result = convertStreamToString(instream);
Log.d(TAG, "---- Result --- " + result);
// now you have the string representation of the HTML request
instream.close();
} else {
Log.d(TAG, "---- Json Activity is --- null ");
}
flag = true;
// net = false;
} catch (Exception e) {
Log.d("Jsomn Activity", "---- Catch --- " + e.toString());
flag = false;
e.printStackTrace();
} finally {
return flag;
}
}
// Method That Will Convert your InputStream to String
private static String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the
* BufferedReader.readLine() method. We iterate until the BufferedReader
* return null which means there's no more data to read. Each line will
* appended to a StringBuilder and returned as String.
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Log.d("Jsomn Activity", "---- Result --- " + sb.toString());
return sb.toString();
}
}
我知道我肯定在这里犯了一些愚蠢的错误,我也在这里尝试了很多方法,但无法解决。请帮助我摆脱困境。
提前致谢。
最佳答案
如果包含空格字符,请使用 URLEncoder.encode
对您的网址进行编码:
StringBuffer sb = new StringBuffer();
sb.append("origin=Chicago,IL&destination=Los+Angeles,CA&
waypoints=Joplin,MO|Oklahoma+City,OK&sensor=false");
URL = URLEncoder.encode(sb.toString(), "UTF-8");
String newURL = "http://maps.googleapis.com/maps/api/directions/json?"+URL;
关于android - 从 maps.googleapis.com 获取 JSON 数据时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14479247/
我正在使用DART构建一个后端服务器。。由于dart没有任何firebase admin sdk,我使用GoogleAPIS来与firebase交互。。对于Firebase认证,我看到它和Google
我需要将图像上传到 Firebase 存储并获取要使用的下载 URL 作为 src对于 我的网络应用程序中的标签。 当我使用 firebase 将图像从浏览器上传到我的存储桶时客户端 SDK,这就是我
我是 GCP 的新手,我正在尝试通过 Terraform 启用一些 API。 variable "gcp_service_list" { description ="Projectof apis"
我的意图: 我想调用 Google 日历的 API acl.list()来自google script使用 UrlFetchApp.fetch()功能。 问题: 我的 Google 脚本在运行时自身具
以下问题:我试图通过 Google Books API 获取信息,我发现 this Example ,但是如果我执行它,它会出现以下错误。 java.net.UnknownHostException:
几天以来,youtube.googleapis.com似乎完全崩溃了。 直到那一刻,嵌入视频(如https://youtube.googleapis.com/v/dQw4w9WgXcQ)都没有问题。目
已经实现了 oauth2.0 并使用范围进行了握手: "https://www.googleapis.com/auth/userinfo.email ", "https://www.googleapi
已实现 oauth2.0 并使用范围完成握手: "https://www.googleapis.com/auth/userinfo.email ", "https://www.googleapis.c
我的用户登录到我的应用程序,应用程序将 idtoken 发送到我的服务器,服务器将请求发送到谷歌,谷歌将用户数据发送到我的服务器,服务器将其放入我的数据库中。工作了 5 个月。现在: 如果我在我的网络
我正在尝试构建一个使用来自以下网址的数据的应用程序:http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=8&q=http%3A%2
使用googleapi时出错 我的错误: (node:7776) UnhandledPromiseRejectionWarning: TypeError: Cannot destructure pro
我正在使用 googleapis 服务获取用户的位置、当前城市名称 + 国家/地区名称: NSString *urlString = [NSString stringWithFormat:@"http
对于新手问题,我深表歉意,因为我是 Android 设备开发的新手(2 天前开始开发)。 不过我明白,要使用谷歌地图的定位服务,当您创建一个项目并且 eclipse 提示您输入 SDK 目标时,您应该
这是我的代码: var http = require('http'); var express = require('express'); var Session = require('express
我有一个应用程序想要嵌入谷歌地图,并且需要将 key 放入配置文件中。所以这就是我所做的: 在 config.js 中 GOOGLE_MAP_KEY = "mykeyofgoogleapi"; 在 i
安装 googleapis pod 时出现错误“没有找到 googleapis 的 podspec。” 我正在尝试安装 pod 'googleapis', :path => '.'但是,它出现以下错误
需要帮助..!我正在研究 Android Transliteration,我的代码中出现以下 3 个错误。 1)GoogleAPI cannot be resolved 2)Translate can
我正在尝试在 Freebase 上查询所有美国县及其地理位置(经度 + 纬度)。我注意到有时查询会起作用,但在其他尝试中它会返回以下内容:。 我尝试更改查询结果限制,但我发现查询失败的限制有所不同;有
我正在学习一个涉及 GoogleMaps API JS 的教程,一切顺利。在尝试构建我自己的应用程序时,我遇到了显示路线的问题。我正在处理的应用程序主要是用 ruby on rails 编写的。
我正在尝试从 .proto 文件编译 GoogleCloudSpeechToTextv1p1beta1 的 C# 库 here . protobuf 编译器可以工作,只是它缺少 googleapis/
我是一名优秀的程序员,十分优秀!