Geocoder 抛出 unable to parse response from server 异常

package com.Muzzzzi.Weather;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class LocationActivity extends Activity implements LocationListener {

private static final String TAG = "Location";

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.location_layout);

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
String provider = LocationManager.NETWORK_PROVIDER;

// Criteria criteria = new Criteria();
// criteria.setAccuracy(Criteria.ACCURACY_FINE);
// criteria.setAltitudeRequired(false);
// criteria.setBearingRequired(false);
// criteria.setCostAllowed(true);
// criteria.setPowerRequirement(Criteria.POWER_LOW);
//
// String provider = locationManager.getBestProvider(criteria, true);

Location location = locationManager.getLastKnownLocation(provider);

updateWithNewLocation(location);

}

private void updateWithNewLocation(Location location) {
// TODO Auto-generated method stub
String latLongString;
String addressString = "string not found";

TextView locationTextView = (TextView) findViewById(R.id.location_layout_textView1);
TextView testTextView = (TextView) findViewById(R.id.location_layout_textView2);

if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" + lng;
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Geocoder gc = new Geocoder(this, Locale.getDefault());



try {
StringBuilder sb = new StringBuilder();
List<Address> addresses = gc.getFromLocation(latitude,
longitude, 10);

testTextView.setText("tryyyyy");
if (addresses.size() > 0) {


Address address = addresses.get(0);
for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
sb.append(address.getAddressLine(i)).append("\n");
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
testTextView.setText("ifff");
}

addressString = sb.toString();
} catch (IOException ioe) {
testTextView.setText("catch!!!");
Log.e(TAG, ioe.toString());
}
} else {
latLongString = "No location found";
}

locationTextView.setText("your position:\n" + latLongString + "\n"
+ addressString);

}

}


异常:65行
Java.io.IOException:Unable to parse response from server

原因:
可能是无法联网返回这个list...
今天早上起来发现用wife又可以运行,用移动net不行。。
解决办法:

貌似这个bug历史悠久啊,参加http://code.google.com/p/android/issues/detail?id=8816
comment#21给出了利用google api 的一种"nasty "的方法。

1.确保你使用google api建立android project
2.在mainifest中包含相关权限,并且导入google api包。
<uses-library android:name="com.google.android.maps" />

3.请求
public static JSONObject getLocationInfo(String address) {
StringBuilder stringBuilder = new StringBuilder();
try {

address = address.replaceAll(" ","%20");

HttpPost httppost = new HttpPost("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false");
HttpClient client = new DefaultHttpClient();
HttpResponse response;
stringBuilder = new StringBuilder();


response = client.execute(httppost);
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) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return jsonObject;
}

4.解析
public static GeoPoint  getLatLong(JSONObject jsonObject) {

Double lon = new Double(0);
Double lat = new Double(0);

try {

lon = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lng");

lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lat");

} catch (Exception e) {
e.printStackTrace();

}

return new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6));
}

5.调用
	JSONObject jo=this.getLocationInfo("hangzhou");
GeoPoint p=this.getLatLong(jo);
testTextView.setText(
p.getLatitudeE6() / 1E6 + "," +
p.getLongitudeE6() /1E6 );


6.address也可以是坐标(经纬度)。
再写一个解析JSON的类就可以实现从坐标到地址的转换,此处略。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值