0

我不确定为什么这个 url 会抛出MalformedURL异常:http%3A%2F%2Fapi.themoviedb.org%2F3%2Fsearch%2Fperson%3Fapi_key%3secret%26query%3Dchristopher_guest

这是我需要使用的api所需的url。http://api.themoviedb.org/3/search/person?api_key=secret&query=christopher_guest

我一直在使用此 url 获取 target host must not be null错误,然后我将我的编码更改为您在下面看到的内容。不确定这里发生了什么,尽管我听说包含下划线的 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;
}

}
4

1 回答 1

0

打印您在最终提交之前形成的字符串以形成 Uri。并将其附加到您的问题中。回答起来会容易得多。

尝试使用

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

原因很简单。如果您使用的是 Uri,您将立即获得异常。

希望这能帮助您快速调试。

于 2012-09-16T07:06:11.617 回答