非法状态异常错误:目标主机不能为null或在参数中设置

时间:2012-09-15 02:43:45

标签: java android

我在我的旧星系上运行我的应用程序,它工作正常,然后我的nexus s,它开始给我一些错误。我在JSONParser中遇到路径错误的非法字符,我使用URLEncoder.encode修复但现在我收到了非法的状态异常错误。我看了http://blog.donnfelker.com/2010/04/29/android-odd-error-in-defaulthttpclient/ 但是我的网址中已经有了http://。我检查了调试器的uri我的httpget。我不太确定我在这里寻找什么。我知道我试图找出一个字符是否已被编码,而不应该像我在上面链接的文章的评论中那样建议,但我不知道如何去做。当我在JSONParser.doInBackground方法中单击httpGet下的uri时,我得到%5BLjava.lang.String%3B%4042b3f010。我是否正确这是URLEncoder.encode的编码表示?我将我转换为String然后编码的StringBuilder类型的URL传递给我的JSONParser.doInBackground。调试器中的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);

1 个答案:

答案 0 :(得分:0)

如果您对此%5BLjava.lang.String%3B%4042b3f010进行网址解码,则会获得[Ljava.lang.String;@42b3f010

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

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

相反,你需要迭代它并下载所有这些,或者只是采取第一个:url[0]。我也将它重命名为urls,只是为了使其显而易见:)