Android call Baidu Map API error Mcode parameter does not exist

Source: Internet
Author: User

Their mobile app used the Baidu Map SDK, hoping to get the coordinates of the mobile phone to reverse to the coordinates of Baidu map.

According to the API document spelled the URL, because it is the mobile side, said to add the Mcode parameter, then my URL looks like this:

http://api.map.baidu.com/geocoder/v2/?ak=xxxxxxx&mcode=sh1code;packname&callback=renderreverse& Location=36.652181,116.870643&output=json&pois=0

First check in the browser, you can return the results, and then wrote a simple httpurlconnection test, on the computer is also possible.

Then put the same data request code httputil.sendhttprequest (connect using httpurlconnection) and URL (above) Migrated to the Android project, to the simulator and the real machine test failed!!!

Return response message: ... mcode parameter does not exist, search the internet for a long time, the most puzzling is the same URL, I also completely correct add the Mcode, the current problem is the computer J2SE code to send the request and the browser directly hit the URL is normal, is not on the phone.

is also tangled half a day, online did not search meaningful answer.

Then I think of the phone-side Android and J2SE is the difference between the JDK, do not know where I have seen a bit of an impression of a word is " Google recommended in Android use HttpClient" Access, Used to request web data in a httpurlconnection way,

Always thought HttpURLConnection and httpclient is only the use of the difference, today encountered the problem is really embarrassing, remind yourself, J2SE and Android SDK still have a difference, try to use Google recommended technology bar.

The problem is solved, the code is simple:

String address = ...//mentioned in the above article//The Android side uses HttpGet request Baidu API data, returns the result to be normal Public Staticstring Sendhttpget (string address) {Try{HttpClient Client=Newdefaulthttpclient (); HttpGet HttpGet=NewHttpGet (address); HttpResponse Res=Client.execute (HttpGet); if(Res.getstatusline (). Getstatuscode () = = 200){                returnEntityutils.tostring (Res.getentity (), "Utf-8"); }                        returnres.getstatusline (). Getreasonphrase (); } Catch(Exception e) {e.printstacktrace (); returne.tostring (); }    }

Android call Baidu Map API error Mcode parameter does not exist

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.