2012-09-16 160 views
0

我不知道爲什麼這個網址是拋出一個異常MalformedURLhttp%3A%2F%2Fapi.themoviedb.org%2F3%2Fsearch%2Fperson%3Fapi_key%3secret%26query%3Dchristopher_guest爲什麼我得到一個MalformedURLException的

這是我需要使用API​​所需的網址。 http://api.themoviedb.org/3/search/person?api_key=secret&query=christopher_guest

我已經越來越目標主機不能使用這個網址,然後我改變了我的編碼,你看到下面有什麼錯誤。不知道這裏發生了什麼,雖然我聽說包含下劃線的url不會在Web瀏覽器之外驗證並導致這些類型的情況。

有關於此的任何想法?

這是我建立的網址

package com.tot.tipofthetongue; 

import android.widget.EditText; 

public class getName { 
static String nameOne = null; 
static String nameTwo = null; 

static StringBuilder personURLOne = new StringBuilder(); 
static StringBuilder personURLTwo = new StringBuilder(); 

public static String personURL = "http://api.themoviedb.org/3/search/person?api_key=secret&query="; 

public static StringBuilder getName1(EditText searchOne){ 
    nameOne = searchOne.getText().toString(); 


    nameOne = nameOne.replace(" ", "_"); 


    personURLOne.append(personURL); 
    personURLOne = personURLOne.append(nameOne); 



    return personURLOne; 

} 

這是我jsonparser,我傳遞的URL。

public class JSONParser extends AsyncTask<String, Void, JSONObject> { 

static InputStream inputStream = null; 
static JSONObject jObject = null; 
static String jSon = ""; 
public String myURL; 
String host; 
HttpRequest request; 
protected JSONObject doInBackground(String... url) { 
    // TODO Auto-generated method stub 

    //Make HTTP Request 
    try { 
     //defaultHttpClient 

     for(int i = 0; i < url.length; i++){ 
      myURL = url[0]; 
      myURL = URLEncoder.encode(myURL, "utf-8"); 
     } 
     HttpGet httpGet = new HttpGet(myURL); 

       //header 
       httpGet.setHeader("Accept", "application/json"); 


       HttpResponse httpResponse = new DefaultHttpClient().execute(new HttpHost(new URL(myURL).getHost()), request); 
       HttpEntity httpEntity = httpResponse.getEntity(); 
       inputStream = httpEntity.getContent(); 

      } catch (UnsupportedEncodingException e){ 
       e.printStackTrace(); 
      } catch (ClientProtocolException e){ 
       e.printStackTrace(); 
      }catch (IOException e){ 
       e.printStackTrace(); 
      } 

      try { 
       BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8); 
       StringBuilder stringBuilder = new StringBuilder(); 
       String line = null; 
       while ((line = reader.readLine()) != null){ 
        stringBuilder.append(line + "\n"); 
       } 
       Log.d("JSON Contents", stringBuilder.toString()); 
       inputStream.close(); 

       jSon = stringBuilder.toString(); 


      } catch (Exception e){ 
       Log.e("Buffer Error", "Error converting result " + e.toString()); 
      } 
      //try to parse the string to JSON Object 
      try { 
       jObject = new JSONObject(jSon); 

      } catch (JSONException e){ 
       Log.e("JSON Parser", "Error parsing data " + e.toString()); 
      } 
      //return JSON String 
      return jObject; 
} 

} 
+1

你有沒有試過它沒有url編碼的網址? – km1

回答

0

打印最終提交之前形成的字符串形成Uri。並附上這個問題。答案會更容易。

嘗試使用

HttpGet(URI uri)
代替
HttpGet(String uri)

原因很簡單。如果您使用Uri,您將立即獲得例外。

希望這可以幫助您快速調試。

相關問題