从目标主机获取JSON对象。错误:目标主机不能为空

时间:2013-11-04 05:08:50

标签: java android json

我正在试图弄清楚JSON现在是如何工作的,所以我想到了比制作一个小JSON阅读项目更好的方法!我希望用户使用EditText和Search按钮供用户使用,并查看用户输入的网站是否有任何JSON,然后将其显示在文本框中。现在我正在测试该网站: itunes.apple.com/search?term =

正如你在我的代码中看到的那样,我只是在没有EditText和Search按钮的情况下设置这些变量,直到我可以使这个东西工作。我收到以下错误:

11-03 20:30:58.545: E/AndroidRuntime(8608): FATAL EXCEPTION: AsyncTask #1
11-03 20:30:58.545: E/AndroidRuntime(8608): java.lang.RuntimeException: An error occured while executing doInBackground()
11-03 20:30:58.545: E/AndroidRuntime(8608):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
11-03 20:30:58.545: E/AndroidRuntime(8608):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
11-03 20:30:58.545: E/AndroidRuntime(8608):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
11-03 20:30:58.545: E/AndroidRuntime(8608):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
11-03 20:30:58.545: E/AndroidRuntime(8608):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-03 20:30:58.545: E/AndroidRuntime(8608):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-03 20:30:58.545: E/AndroidRuntime(8608):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-03 20:30:58.545: E/AndroidRuntime(8608):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-03 20:30:58.545: E/AndroidRuntime(8608):     at java.lang.Thread.run(Thread.java:856)
11-03 20:30:58.545: E/AndroidRuntime(8608): Caused by: java.lang.IllegalStateException:
 Target host must not be null, or set in parameters. scheme=null, host=null, path=itunes.apple.com/search

我认为这是因为以下行: HttpResponse r = client.execute(get); 但我不知道如何将主机更正为null。我觉得它与问号或其他东西有关。或许我只是以错误的方式接近这个。请考虑这是我的第一个使用JSON的项目。也许我需要一个关于使用JSON的简单教程,以便我彻底理解它。谢谢!

MainActivity.java

public class MainActivity extends Activity implements OnClickListener {

TextView fetchText;
EditText httpEntryBox,jsonEntryBox;
Button search_button_http, search_button_json;
HTTP_Fetcher http_fetch;

HttpClient client;
final static String URL = "itunes.apple.com/search?term=";
JSONObject json;
InputMethodManager imm;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    fetchText = (TextView) findViewById(R.id.fetchText);

    client = new DefaultHttpClient();
    new Read().execute("artistName");


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public JSONObject search( String searchItem ) throws ClientProtocolException, IOException, JSONException, URISyntaxException{
    StringBuilder url = new StringBuilder(URL);
    url.append(searchItem);

    String finished_url = url.toString();

    HttpGet get = new HttpGet(finished_url);
    HttpResponse r = client.execute(get);

    int status = r.getStatusLine().getStatusCode();

    if (status == 200){
        HttpEntity e = r.getEntity();
        String data = EntityUtils.toString(e);
        JSONArray array = new JSONArray(data);
        return array.getJSONObject(0);
    }else{
        Log.e("Search","fetch error");
        return null;
    }


}

public class Read extends AsyncTask<String, Integer, String>{

    @Override
    protected String doInBackground(String... params) {
        try {
            json = search("lilwayne");
            return json.getString(params[0]);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        fetchText.setText(result);
    }

}
}

1 个答案:

答案 0 :(得分:3)

我认为你错过了 http 计划,所以不是:

final static String URL = "itunes.apple.com/search?term=";

放:

final static String URL = "http://itunes.apple.com/search?term=";

修改
即使这样可行,解析JSON也会失败,因为这一行:JSONArray array = new JSONArray(data); - 这假定响应是一个数组,但它是一个包含两个条目的JSONObject:resultsCountresults 。您需要将其设为JSONObjectJSONObject obj = new JSONObject(data);。互联网上充满了android json parsing教程。这是一个relevant article