【问题标题】:Illegal State Exception error: target host must not be null or set in parametersIllegal State Exception 错误:目标主机不得为空或在参数中设置
【发布时间】:2012-09-15 02:43:44
【问题描述】:

我在旧的 Galaxy s 上运行我的应用程序,它运行良好,然后我在我的 nexus s 上运行我的应用程序,它开始给我一些错误。我在使用 URLEncoder.encode 修复的 JSONParser 中的路径错误中遇到了非法字符,但现在我遇到了非法状态异常错误。我看了这里http://blog.donnfelker.com/2010/04/29/android-odd-error-in-defaulthttpclient/ 但我的网址中已经有 http://。我在调试器中检查了我的 httpget 的 uri。我不确定我在这里寻找什么。我知道我正在尝试查找是否已对我在上面链接到的文章的 cmets 中不应该建议的字符进行编码,但我不知道如何去做。当我在 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);

【问题讨论】:

    标签: java android


    【解决方案1】:

    如果你对这个%5BLjava.lang.String%3B%4042b3f010进行URL解码,你会得到[Ljava.lang.String;@42b3f010

    这很可疑,它不是 URL。当你在一个数组上调用toString 时,你会得到一个带有左括号的有趣字符串。例如,一个整数数组会打印出类似 [I@33f42b49 的内容。

    这是因为url 在您的doInBackGround 方法中实际上是一个字符串数组,而不是字符串,因为doInBackground 采用可变数量的字符串参数:doInBackground(String... 省略号表示带有可变参数的方法。所以你根本不想打电话给toString

    相反,您需要遍历它并下载所有这些,或者只使用第一个:url[0]。我也将它重命名为urls,只是为了让它更明显:)

    【讨论】:

    • 我更新了我的代码,请参阅更新部分。我仍然收到此错误:09-14 22:28:28.459: E/AndroidRuntime(31892): Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=api.themoviedb.org/3/search/…我是否正确采纳了你的建议?
    • 好像没有scheme,但你说的是“http”?尝试打印出来...
    猜你喜欢
    • 2013-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多