值传入AsyncTask在android中采用对象格式
我以这种方式执行AsyncTask:值传入AsyncTask在android中采用对象格式,android,Android,我以这种方式执行AsyncTask: public class GecodeFuncation { public static String getLatLongByURL(String requestURL) { URL url; String response = ""; try { url = new URL(requestURL); HttpURLConnection conn = (HttpURLConnection) url
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;
}
}
下面是我的异步任务
:
new GeocodeImplementation().execute("D-109 3rd floor ShakarPur Laxmi Nagar New Delhi 110092".replace(" ","%20"));
class GeocodeImplementation扩展了异步任务{
ProgressDialog=新建ProgressDialog(DataEntry.this);
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
setMessage(“请稍候…”);
对话框。setCanceledOnTouchOutside(false);
dialog.show();
}
@凌驾
受保护字符串[]doInBackground(字符串…参数){
字符串响应;
试一试{
response=gecodefunction.getLatLongByURL(“http://maps.google.com/maps/api/geocode/json?address=“+params+”&sensor=false”);
Log.d(“响应”,“响应+响应”);
返回新字符串[]{response};
}捕获(例外e){
返回新字符串[]{“error”};
}
}
@凌驾
受保护的void onPostExecute(字符串…结果){
试一试{
JSONObject JSONObject=新的JSONObject(结果[0]);
double lng=((JSONArray)jsonObject.get(“结果”)。getJSONObject(0)
.getJSONObject(“几何体”).getJSONObject(“位置”)
.getDouble(“液化天然气”);
double lat=((JSONArray)jsonObject.get(“结果”)。getJSONObject(0)
.getJSONObject(“几何体”).getJSONObject(“位置”)
.getDouble(“lat”);
对数d(“纬度”,“纬度+纬度”);
对数d(“经度”,“液化天然气”);
}捕获(JSONException e){
e、 printStackTrace();
}
if(dialog.isShowing()){
dialog.dismise();
}
}
}
当我将地址传递给AsyncTask时,其URL变为:
所以我无法得到应有的回应
因此,我可以得到响应。请告诉我哪里做错了。执行与此相同的任务: 答复=
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();
}
}
}
params是一个字符串…,这意味着它是一个varargs(字符串数组),而不是一个字符串,因此您必须使用params[0]访问存储在第一个位置的url,方法如下:response=gecodefunction.getLatLongByURL(“您的url”+params[0]+“&sensor=false”);让一切保持不变。
GecodeFuncation.getLatLongByURL("http://maps.google.com/maps/api/geocode/json?address="+params[0]+"&sensor=false");