我正在尝试使用json解析来获取城市状态和邮政编码。 我正在使用google api。
我在两个设备android 2.3和android 4.1.2上调试下面的代码。 在android 2.3中,它的工作正常。但在android 4.1.2中,我遇到了运行时错误。
这是我的代码
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的源。 请解决我的问题
答案 0 :(得分:1)
请参考https://stackoverflow.com/a/18259495/1944782,附加url参数并根据您的要求制作网址。执行此操作后,请将您的代码放在AsyncTask Background中,并从您的主Activity调用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;
}
答案 1 :(得分:1)
我面临同样的问题。
这一个可以帮助你。在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;
}
答案 2 :(得分:0)
我希望这可以帮助您,使用此代码获得精确地址位置
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;
}