简体   繁体   中英

why am I getting a MalformedURLException

I am not sure why this url is throwing a MalformedURL exception: http%3A%2F%2Fapi.themoviedb.org%2F3%2Fsearch%2Fperson%3Fapi_key%3secret%26query%3Dchristopher_guest

This is the url required by the api that I need to use. http://api.themoviedb.org/3/search/person?api_key=secret&query=christopher_guest

I have been getting target host must not be null errors using this url then I changed my coded to what you are seeing below. Not sure whats going on here although I have heard urls that contain underscores dont validate outside of web browsers and cause these types of situations.

Any ideas around this?

This is where I build the url

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;

}

And this is my jsonparser that I pass that url to.

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;
}

}

Print the String you formed before final submission to form Uri. And attach this to your question. It would be much easier to answer.

Try using

HttpGet(URI uri)
instead of
 HttpGet(String uri) 

The reason is pretty simple. If you are using Uri, you will get immediately the Exception.

Hope this will help you to debug quickly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM