2016-03-22 6 views
-1
public class GecodeFuncation { 

public static String getLatLongByURL(String requestURL) { 
    URL url; 
    String response = ""; 
    try { 
     url = new URL(requestURL); 

     HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
     conn.setReadTimeout(15000); 
     conn.setConnectTimeout(15000); 
     conn.setRequestMethod("GET"); 
     conn.setDoInput(true); 
     conn.setRequestProperty("Content-Type", 
       "application/x-www-form-urlencoded"); 
     conn.setDoOutput(true); 
     int responseCode = conn.getResponseCode(); 

     if (responseCode == HttpsURLConnection.HTTP_OK) { 
      String line; 
      BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
      while ((line = br.readLine()) != null) { 
       response += line; 
      } 
     } else { 
      response = ""; 
     } 

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

でのオブジェクトギである:以下値のパスは、私がこのようにAsyncTaskをexcutingいアンドロイド

new GeocodeImplementation().execute("D-109 3rd floor ShakarPur Laxmi Nagar New Delhi 110092".replace(" ","%20")); 

は私AsyncTask次のとおりです。

class GeocodeImplementation extends AsyncTask<String, Void, String[]> { 
     ProgressDialog dialog = new ProgressDialog(DataEntry.this); 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      dialog.setMessage("Please wait..."); 
      dialog.setCanceledOnTouchOutside(false); 
      dialog.show(); 
     } 

     @Override 
     protected String[] doInBackground(String... params) { 
      String response; 
      try { 
       response = GecodeFuncation.getLatLongByURL("http://maps.google.com/maps/api/geocode/json?address="+params+"&sensor=false"); 
       Log.d("response",""+response); 
       return new String[]{response}; 
      } catch (Exception e) { 
       return new String[]{"error"}; 
      } 
     } 

     @Override 
     protected void onPostExecute(String... result) { 
      try { 
       JSONObject jsonObject = new JSONObject(result[0]); 

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

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

       Log.d("latitude", "" + lat); 
       Log.d("longitude", "" + lng); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      if (dialog.isShowing()) { 
       dialog.dismiss(); 
      } 
     } 
    } 

私は、AsyncTaskにアドレスを渡しますそのURLは次のようになります。

http://maps.google.com/maps/api/geocode/json?address=[Ljava.lang.String;@41fff330&sensor=false

だから私はそれは私が私が間違っているのどこレスポンスが私に教えてください得ることができます

http://maps.google.com/maps/api/geocode/json?address=D-109%203rd%20floor%20ShakarPur%20Laxmi%20Nagar%20New%20Delhi%20110092&sensor=false

する必要がありながら、応答を取得することができません。

+0

のparams Stringですparams [0] –

+0

taskeをexcuteする方法 –

+0

このようにしてください: レスポンス= GecodeFuncation.getLatLongByURL( "あなたのURL" + params [0] + "&sensor = false"あなたのURLにアクセスするには、 ); すべてを同じままにします。 –

答えて

0

このように同じタスクを実行します。あなたが持っている、それは可変引数(文字列の配列)、ではない文字列で何を意味するのか、して...

応答=

GecodeFuncation.getLatLongByURL("http://maps.google.com/maps/api/geocode/json?address="+params[0]+"&sensor=false"); 
関連する問題