我在我的舊銀河系上運行我的應用程序,它運行良好,然後我聯繫了它,它開始給我一些錯誤。我在使用URLEncoder.encode修復的JSONParser中遇到了路徑錯誤中的非法字符,但現在我得到了非法狀態異常錯誤。我在這裏看到http://blog.donnfelker.com/2010/04/29/android-odd-error-in-defaulthttpclient/ 但我已經有http://在我的網址。我檢查了我的httpget的uri的調試器。我不完全確定我在這裏尋找什麼。我知道我試圖找出一個字符是否被編碼,應該不是我在上面鏈接的文章的評論中所建議的,但我不知道如何去做。當我在JSONParser.doInBackground方法中的httpGet下點擊我的uri時,我得到%5BLjava.lang.String%3B%4042b3f010。我糾正這是URLEncoder.encode的編碼表示形式嗎?我向我的JSONParser.doInBackground傳入一個類型爲StringBuilder的url,我將其轉換爲String,然後進行編碼。調試器中的myURL條目與uri相同:%5BLjava.lang.String%3B%4042b3f010。我是否正確地做這件事。感謝任何幫助。這些是我認爲是我的代碼的相關部分:非法狀態異常錯誤:目標主機不能爲空或在參數中設置
public class JSONParser extends AsyncTask<String, Void, JSONObject> {
static InputStream inputStream = null;
static JSONObject jObject = null;
static String jSon = "";
protected JSONObject doInBackground(String... url) {
// TODO Auto-generated method stub
//Make HTTP Request
try {
//defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
String myURL = url.toString();
myURL = URLEncoder.encode(myURL, "utf-8");
HttpGet httpGet = new HttpGet(myURL);
//header
httpGet.setHeader("Accept", "application/json");
HttpResponse httpResponse = httpClient.execute(httpGet);
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;
}
}
這是我如何建立,我通過給解析器的網址:
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=bb0b6d66c2899aefb4d0863b0d37dc4e&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;
protected JSONObject doInBackground(String... url) {
// TODO Auto-generated method stub
//Make HTTP Request
try {
//defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
for(int i = 0; i < url.length; i++){
myURL = url[0];
myURL = URLEncoder.encode(myURL, "utf-8");
}
HttpGet httpGet = new HttpGet(myURL);
我更新了我的代碼,請參閱更新部分。我仍然得到這個錯誤,雖然:09-14 22:28:28.459:E/AndroidRuntime(31892):引起:java.lang.IllegalStateException:目標主機不能爲空,或在參數中設置。 scheme = null,host = null,path = http://api.themoviedb.org/3/search/person?api_key = bb0b6d66c2899aefb4d0863b0d37dc4e&query = christopher_guest我是否正確使用了您的建議? – user881667
看起來這個計劃不見了,但你說它是「http」?嘗試打印出來... – spacemanaki