2015-10-06 202 views

回答

1

當你發送的凌空請求時,它會叫你回來,如果請求成功或發生錯誤:

所以你需要調用包含應得到響應後執行的代碼的方法:

在這裏,我有評論,你可以調用這個方法:

JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST, url, js, new Response.Listener<JSONObject>() { 

       @Override 
       public void onResponse(JSONObject response) { 
        Log.e(TAG, "Successfully signed in : " + response.toString()); 
        //put your code here 
// added method call 
parseJson(); 
       } 
      }, new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        String json = null; 
        Log.e(TAG, "Error at sign in : " + error.getMessage()); 
        // put your code here 
       } 
      }); 

編輯

對於JSON解析後執行代碼:

處理程序:IncomingHandler

private static class IncomingHandler extends Handler { 
     private WeakReference<MainActivity> yourActivityWeakReference; 

     public IncomingHandler(MainActivity yourActivity) { 
      yourActivityWeakReference = new WeakReference<>(yourActivity); 
     } 

     @Override 
     public void handleMessage(Message message) { 
      if (yourActivityWeakReference != null) { 
       MainActivity yourActivity = yourActivityWeakReference.get(); 

       switch (message.what) { 
        case 0: 

         // add your code here 
         break; 
       } 
      } 
     } 
    } 

onCreate()

// Declaration of handler 
private IncomingHandler incomingHandler; 
// initialize handler 
     incomingHandler=new IncomingHandler(MainActivity.this); 

樣本解析JSON方法創建它的實例,並使用處理程序發送消息:

public void parseJson() { 
     for (int i = 0; i < 50; i++) { 
      System.out.println("Printing :" + i); 
     } 
     incomingHandler.sendEmptyMessage(0); 
    } 

謝謝。!!

+0

你爲什麼降低我?我提到,我是初學者,那麼也 –

+0

@Rohan Bundelkhandi:哦,人..!我沒有貶低你,而是我已經給你一個答案..! – AndiGeeky

+0

以前我試過你的代碼,但它不起作用 –