Siva
Siva

Reputation: 457

JSON Parsing runtime error source not found in android

I'm trying to do json parse for getting city state and zipcode. I'm using google api for this.

I debug the below code on two devices android 2.3 and android 4.1.2. in android 2.3 its works fine.but in android 4.1.2 i got runtime error.

here is a my code

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();
}

on the HttpResponse httpResponse = httpClient.execute(httpPost); line i got an runtime error Source not fount from ActivityThread.performLaunchActivity. please sort it out my problem

Upvotes: 0

Views: 942

Answers (2)

learner
learner

Reputation: 3110

I hope this is Help you,You get Exact Address location using this code

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;
}

Upvotes: 0

Karan Maru
Karan Maru

Reputation: 1001

please refer https://stackoverflow.com/a/18259495/1944782 , append url parameter and make url according your requirement. After doing this, please put your code in the AsyncTask Background and call AsyncTask from your main Activity. Hope your problem would be solved after doing this.

Update

please refer this code:

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;

}

Upvotes: 1

Related Questions