1

我已经实现了如下所示的所有内容,但是 HTTP 在 Android 2.2 中无法正常工作,但在 Android 4.2 中可以正常工作。

它在以下位置崩溃:

 HttpGet get = new HttpGet(s.trim());

代码是:

 int SDK_INT = android.os.Build.VERSION.SDK_INT;
            if (SDK_INT>8)
            {
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                StrictMode.setThreadPolicy(policy);
            }
            HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
            DefaultHttpClient client = new DefaultHttpClient();
            SchemeRegistry registry = new SchemeRegistry();
            SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
            socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
            registry.register(new Scheme("https", socketFactory, 443));
            SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient(mgr,client.getParams());
            HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
             // DefaultHttpClient client = new DefaultHttpClient();

                // ssl code



                    try 
                    {
                        String s= "http://maps.googleapis.com/maps/api/directions/json?origin="+etBookPick.getText()+"&destination="+etBookDrop.getText()+"&sensor=false";

                        HttpGet get = new HttpGet(s.trim());

                        try 
                        {
                            InputStream is = null;
                            JSONObject jObj = null;
                            String jsonn = "";
                            HttpResponse hresponse = httpClient.execute(get);
                            HttpEntity httpEntity = hresponse.getEntity();
                            is = httpEntity.getContent();       


                            try {
                                BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 8);
                                StringBuilder sb = new StringBuilder();
                                String line = null;
                                while ((line = reader.readLine()) != null) {
                                    sb.append(line + "\n");
                                }
                                is.close();
                                jsonn = sb.toString();
                            } catch (Exception e) {
                                Log.e("Buffer Error", "Error converting result " + e.toString());
                            }

                            // try parse the string to a JSON object
                            try {
                                jObj = new JSONObject(jsonn);
                            } catch (JSONException e) {
                                Log.e("JSON Parser", "Error parsing data " + e.toString());
                            }


                            JSONArray routeArray = jObj.getJSONArray("routes");
                            JSONObject routes = routeArray.getJSONObject(0);

                            JSONArray newTempARr = routes.getJSONArray("legs");
                            JSONObject newDisTimeOb = newTempARr.getJSONObject(0);

                            JSONObject distOb = newDisTimeOb.getJSONObject("distance");
                            JSONObject timeOb = newDisTimeOb.getJSONObject("duration");


                            Toast.makeText(BookCab.this, distOb.getString("text"), Toast.LENGTH_LONG).show();
                             distanceinKm=  distOb.getString("text");
                            distanceinKm = distanceinKm.replace("km", "").trim();




                    } catch (ParseException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

替代答案是:

String s= "http://maps.googleapis.com/maps/api/directions/json?origin="+etBookPick.getText()+"&destination="+etBookDrop.getText()+"&sensor=false";
                        s= s.replace(" ", "%20");
4

2 回答 2

1

试试下面

    StringBuilder urlString = new StringBuilder(); 
    urlString.append("http://maps.googleapis.com/maps/api/directions/json?origin="); 
    urlString.append("Chandigarh"); // use etBookPick.getText() here
    urlString.append("&destination=");//to 
    urlString.append("Mohali"); // use etBookDrop.getText() here
    urlString.append("&sensor=false");
    try {
    URL  url = new URL(urlString.toString());
    Log.i("............",""+url);
    } catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

从讨论中您没有使用线程或异步任务。

您应该使用threadsasynctask来回网络相关操作。

于 2013-10-29T06:43:03.567 回答
0

HttpGet如果 URI 无效则抛出java.lang.IllegalArgumentException,因此这意味着您提供的 URI 无效。您可以为此使用 URI.Builder。看到这个这个

于 2013-10-29T06:48:55.640 回答