私はAndroidアプリを作っていますが、githubリポジトリのpackage.json
からパラメータ(logo
)を取得するにはJSONObjectRequestを作成する必要があります。JSONObjectRequestとの同期(Android)
これは私がSingletonパターンを次のGlobal
クラスの変数(logo
)を定義しているので、私は私のプログラムでどこでもアクセスすることができ、私はJSONリクエスト
public void askLogoPath(String url) {
JsonObjectRequest req = new JsonObjectRequest(url,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
if (response.has("logo"))
Global.getInstance().setLogo(response.getString("logo"));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
// Adding request to request queue
QueueController.getInstance().addToRequestQueue(req);
}
を作る方法です。このリクエストをした後、私はロゴの値で変数を設定したいと思います。
これは私が前の関数と呼ぶコードの一部です。
if (full_name != null) {
String url = "https://raw.githubusercontent.com/" + full_name + "/master/";
this.askLogoPath(url + "package.json");
this.setLogoPath(Global.getInstance().getLogo());
}
私はonResponse
一部が実行される最後のものであることに気づいたので、私はsetLogoPath(Global.getInstance.getLogo())
ロゴを行うときに、私は前の行にlogoPath
を設定することはできません、まだnullでいます。これが実行されるまで待機したいので、nullにはなりません。
私はこれを行うことができますので、それを同期させる方法はありますか?
は、あなたが同期的にそれをやってみたいので、あなたはそれが非同期でAsyncTaskを使用してやってもかまわない場合は、それが
new Thread(new Runnable() {
@Override
public void run() {
//set your logo
}
}).start();
を終了されるまでのプロセスをロックするスレッドを使用することができ、非常に