3

我正试图弄清楚 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); 但我不知道我应该如何纠正主机为空。感觉跟问号什么的有关系。或者也许我只是以错误的方式接近这个。请考虑到这是我的第一个 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);
    }

}
}
4

1 回答 1

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. 您需要将其设为JSONObject: JSONObject obj = new JSONObject(data);。互联网上到处都是android json parsing教程。这是一篇相关文章

于 2013-11-04T05:13:39.737 回答