《2022年MARS-android视频教程代码-第三季第五集 .pdf》由会员分享,可在线阅读,更多相关《2022年MARS-android视频教程代码-第三季第五集 .pdf(6页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、MARS-Android 视频教程实例源代码第三季第六集(定位操作MARS 老师变相实现的那个)注:该例需要在Android2.2 以下的模拟器或手机上去实现,否则就需要加入StrictMode 语句代码, 是因为 android2.3 以上版本加入了在访问网络、访问本地磁盘等费时操作时不能在主线程上实现的功能。AVD 版本Build SDK 和 Mini SDK Location01 activity_main.xml MainActivity.java package mars.location06; import java.io.BufferedReader; import java.i
2、o.InputStreamReader; import .URLEncoder; import org.apache.http.HttpEntity; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 6 页 - - - - - - - - - import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods
3、.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import com.google.gson.Gson; import android.os.Bundle; import android.os.StrictMode; import android.annotation.SuppressLint; import android.app.Activity; import android.text.style.StrikethroughSpan; import android.view.Menu; import andr
4、oid.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity private Button geobutton=null; /SuppressLint( NewApi, NewApi, NewApi, NewApi, NewApi, NewApi, NewApi, NewApi, NewApi, NewApi, NewApi, NewApi ) Override public void onCrea
5、te(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build(); /StrictMode.setVmPolicy(new StrictMode.VmPolicy.Build
6、er().detectLeakedSqlLiteObjects().penaltyLog().penaltyDeath().build(); geobutton=(Button)findViewById(R.id.buttonId); geobutton.setOnClickListener(new geoButtonOnClickListener(); private class geoButtonOnClickListener implements OnClickListener public void onClick(View v) System.out.println(geoButto
7、nListener); String url=http:/ /String url=http:/74.125.128.95/maps/api/geocode/json?+URLEncoder.encode(latlng=40.714224,-73.961452&sensor=false); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 6 页 - - - - - - - - - HttpClient httpClient=new DefaultHttpClient();
8、 String responseData=; try HttpResponse response=httpClient.execute(new HttpGet(url); HttpEntity entity=response.getEntity(); BufferedReader bufferReader=new BufferedReader(new InputStreamReader(entity.getContent(); String line=; while(line=bufferReader.readLine()!=null) responseData=responseData+li
9、ne; catch(Exception e) e.printStackTrace(); Gson gson=new Gson(); TestResult testResult=gson.fromJson(responseData,TestResult.class); System.out.println(responseData); Override public boolean onCreateOptionsMenu(Menu menu) getMenuInflater().inflate(R.menu.activity_main, menu); return true; Result.ja
10、va package mars.location06; publicclass Result private String types; private String formatted_address; /* * return the types */public String getTypes() returntypes; /* * param types the types to set */publicvoid setTypes(String types) this. types = types; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - -
11、 - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 6 页 - - - - - - - - - /* * return the formatted_address */public String getFormatted_address() returnformatted_address; /* * param formatted_address the formatted_address to set */publicvoid setFormatted_address(String formatted_address) this. formatted_addr
12、ess = formatted_address; TestResult.java package mars.location06; import java.util.List; publicclass TestResult private String status; private List results; /* * return the status */public String getStatus() returnstatus; /* * param status the status to set */publicvoid setStatus(String status) this
13、. status = status; /* * return the results */public List getResults() returnresults; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 6 页 - - - - - - - - - /* * param results the results to set */publicvoid setResults(List results) this. results = results; /* (no
14、n-Javadoc) * see java.lang.Object#toString() */Overridepublic String toString() / TODO Auto-generated method stubSystem.out .println(aaaaaa); returnTestResult results=+results+,status=+status+; AndroidManifest.xml 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 6 页 - - - - - - - - - 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 6 页 - - - - - - - - -