2017-03-22 3 views
-4
package com.monishn.android.volley; 

import android.content.Context; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.app.Activity; 
import android.app.ProgressDialog; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 
import com.android.volley.Response; 
import com.android.volley.VolleyError; 
import com.android.volley.VolleyLog; 
import com.android.volley.toolbox.JsonArrayRequest; 
import com.android.volley.toolbox.JsonObjectRequest; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

public class MainActivity extends AppCompatActivity { 

    // json array response url 
    private String urlJsonArry = "www.shaoniiuc.com/my_json"; 

    private static String TAG = MainActivity.class.getSimpleName(); 
    private Button btnMakeObjectRequest, btnMakeArrayRequest; 

    // Progress dialog 
    private ProgressDialog pDialog; 

    private TextView txtResponse; 

    // temporary string to show the parsed response 
    private String jsonResponse; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     btnMakeArrayRequest = (Button) findViewById(R.id.btnArrayRequest); 
     txtResponse = (TextView) findViewById(R.id.txtResponse); 
     pDialog = new ProgressDialog(this); 
     pDialog.setMessage("Please wait..."); 
     pDialog.setCancelable(false); 

     btnMakeArrayRequest.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
// making json array request 
       makeJsonArrayRequest(); 
      } 
     }); 

    } 

    /** 
    * Method to make json array request where response starts with [ 
    * */ 
    private void makeJsonArrayRequest() { 

     showpDialog(); 


     JsonArrayRequest req = new JsonArrayRequest(urlJsonArry,new Response.Listener<JSONArray>(){ 

        @Override 
        public void onResponse(JSONArray response) { 
         Log.d(TAG, response.toString()); 

         try { 
          // Parsing json array response 
          // loop through each json object 
          jsonResponse = ""; 
          for (int i = 0; i < response.length(); i++) { 

           JSONObject person = (JSONObject) response.get(i); 

           String id = person.getString("id"); 
           String name = person.getString("name"); 

           jsonResponse += "Name: " + name + "\n\n"; 
           jsonResponse += "Id: " + id + "\n\n"; 

          } 
          txtResponse.setText(jsonResponse); 

         } catch (JSONException e) { 
          e.printStackTrace(); 
          Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show(); 
         } 

         hidepDialog(); 
        } 
      }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       VolleyLog.d(TAG, "Error: " + error.getMessage()); 
       Toast.makeText(getApplicationContext(),error.getMessage(), Toast.LENGTH_SHORT).show(); 
       hidepDialog(); 
      } 
     }); 

// Adding request to request queue 
     AppController.getInstance().addToRequestQueue(req); 
    } 

    private void showpDialog() { 
     if (!pDialog.isShowing()) 
      pDialog.show(); 
    } 

    private void hidepDialog() { 
     if (pDialog.isShowing()) 
      pDialog.dismiss(); 
    } 
} 

LogcatデータgetApplicationContext()」

08:59:19.981 27919-27919/com.monishn.android.volley E/AndroidRuntime: FATAL EXCEPTION: main 
                       java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference 
                        at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:107) 
                        at com.monishn.android.volley.AppController.getRequestQueue(AppController.java:33) 
                        at com.monishn.android.volley.AppController.addToRequestQueue(AppController.java:46) 
                        at com.monishn.android.volley.MainActivity.makeJsonArrayRequest(MainActivity.java:107) 
                        at com.monishn.android.volley.MainActivity.access$000(MainActivity.java:23) 
                        at com.monishn.android.volley.MainActivity$1.onClick(MainActivity.java:53) 
                        at android.view.View.performClick(View.java:5207) 
                        at android.view.View$PerformClick.run(View.java:21168) 
                        at android.os.Handler.handleCallback(Handler.java:746) 
                        at android.os.Handler.dispatchMessage(Handler.java:95) 
                        at android.os.Looper.loop(Looper.java:148) 
                        at android.app.ActivityThread.main(ActivityThread.java:5443) 
                        at java.lang.reflect.Method.invoke(Native Method) 
                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
    03-22 08:59:21.847 27919-27919/com.monishn.android.volley 
+1

なぜあなたは 'getApplicationContext'を使用していますか?すでに 'Activity'に入っているので' this'を使うか、コンテキストをローカル変数として保存してそれを使用してください。 –

+0

あなたのAppControllerクラスを投稿する - それが例外を投げていることです。 AppController.getInstance()。addToRequestQueue(req); – user681574

+0

これは一般的な質問です。ここでの質問に先立ってGoogleでさらに検索してみてください。 –

答えて

1
public class AppController extends Application { 

    protected static AppController sInstance; 
    private RequestQueue mRequestQueue; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 

     mRequestQueue = Volley.newRequestQueue(this); 
     sInstance = this; 
    } 

    public synchronized static AppController getInstance() { 
     return sInstance; 
    } 

    public RequestQueue getRequestQueue() { 
     return mRequestQueue; 
    } 
} 

マニフェストで(これはエラーでなければなりません)

<application 
     android:name=".AppController" 
     android:allowBackup="true" 
     .... 

あなたはNULLコンテキストをreturingされているのでin AppController

リクエストを追加このようなeeue

AppController.getInstance().getRequestQueue().add(req) 
+0

ありがとうございました。 – Anupam

関連する問題