我在我的旧 Galaxy s 上运行我的应用程序,它运行良好,然后我在我的 nexus s 上运行我的应用程序,它开始给我一些错误。我在使用 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 的编码表示?我将一个 StringBuilder 类型的 url 传递给我的 JSONParser.doInBackground,我将其转换为 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;
}
}
这就是我构建传递给解析器的 url 的方式:
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);