Java 在android中找不到JSON解析运行时错误源
我正在尝试使用json解析来获取city state和zipcode。 我用谷歌api来做这个 我在android 2.3和android 4.1.2两个设备上调试了下面的代码。 在安卓2.3中,它工作正常。但在安卓4.1.2中,我遇到了运行时错误 这是我的密码Java 在android中找不到JSON解析运行时错误源,java,android,json,google-maps,Java,Android,Json,Google Maps,我正在尝试使用json解析来获取city state和zipcode。 我用谷歌api来做这个 我在android 2.3和android 4.1.2两个设备上调试了下面的代码。 在安卓2.3中,它工作正常。但在安卓4.1.2中,我遇到了运行时错误 这是我的密码 String url = "http://maps.google.com/maps/api/geocode/json?address="+address+"&sensor=true"; JSONParser jparser =
String url = "http://maps.google.com/maps/api/geocode/json?address="+address+"&sensor=true";
JSONParser jparser = new JSONParser();
JSONObject jobject = jparser.getJSONFromUrl(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();
}
在HttpResponse上,HttpResponse=httpClient.execute(httpPost)代码>行我从ActivityThread.performLaunchActivity获得一个运行时错误源。
请解决我的问题请参考,添加url参数,并根据您的要求制作url。完成此操作后,请将代码放在AsyncTask后台,并从主活动调用AsyncTask。希望这样做后你的问题会得到解决
更新
请参阅此代码:
public JSONObject getAddressData() {
HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?address="+address+"&sensor=true");
HttpClient client = new DefaultHttpClient();
HttpResponse response;
StringBuilder stringBuilder = new StringBuilder();
try {
response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while ((b = stream.read()) != -1) {
stringBuilder.append((char) b);
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
JSONObject jsonObject = new JSONObject();
try {
jsonObject = new JSONObject(stringBuilder.toString());
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject;
}我也面临同样的问题。
这个对你有帮助。在AsyncTask
doInBackground
方法中使用下面的函数并检查响应,然后可以在onPostExecute
方法中解析它
public static String getResponefromURL(String url) {
InputStream is = null;
String result = "";
// http post
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url.trim());
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// convert response to string
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();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
return result;
}
我希望这是帮助你,你得到确切的地址位置使用此代码
公共字符串gpsadd(){
试一试{
如果(isGPS){
Geocoder gc=新的Geocoder(Home.this,Locale.getDefault());
尝试
{
addflg=0;
lat=位置。getLatitude();
lng=location.getLongitude();
布尔lop=真;
while(lop){
列表地址=gc.getFromLocation(lat,lng,1);
如果(地址.size()>0)
{
lop=假;
addflg=1;
地址=地址。获取(0);
s_addr=address.getAddressLine(0)+++address.getAddressLine(1)+++address.getAddressLine(2);
disp=“Location\n”+address.getAddressLine(0)+“\n”+address.getAddressLine(1)+“\n”+address.getAddressLine(2);
}
}
}
捕获(例外e)
{
e、 printStackTrace();
}
}否则{
显示(“GPS未启用”);
}
}捕获(例外e){
e、 printStackTrace();
}
返回disp;
}
我照你说的做了,但还是在同一个地方犯了同样的错误。
public String gpsadd() {
try {
if(isGPS) {
Geocoder gc = new Geocoder(Home.this, Locale.getDefault());
try
{
addflg=0;
lat=location.getLatitude();
lng=location.getLongitude();
boolean lop=true;
while(lop) {
List<Address> addresses = gc.getFromLocation(lat, lng, 1);
if (addresses.size() > 0)
{
lop=false;
addflg=1;
address = addresses.get(0);
s_addr=address.getAddressLine(0)+" "+address.getAddressLine(1)+" "+address.getAddressLine(2);
disp="Location\n "+address.getAddressLine(0)+"\n"+address.getAddressLine(1)+"\n"+address.getAddressLine(2);
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
} else {
display("GPS not Enabled");
}
} catch(Exception e) {
e.printStackTrace();
}
return disp;
}