我不确定为什么这个网址会引发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
我一直在使用此网址获取目标主机不能 null 错误然后我将我的编码更改为您在下面看到的内容。虽然我听说包含下划线的网址不会在网络浏览器之外验证并导致这些类型的情况,但不确定这里会发生什么。
有关于此的任何想法吗?
这是我构建网址的地方
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。
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;
}
}
答案 0 :(得分:0)
打印您在最终提交前形成的字符串以形成Uri。并将此附加到您的问题。这会更容易回答。
尝试使用
HttpGet(URI uri)
代替HttpGet(String uri)
原因很简单。如果您正在使用Uri,您将立即获得例外。
希望这可以帮助您快速调试。