使用ArrayList作为属性的Java类

时间:2014-07-03 12:13:38

标签: java android exception arraylist

我正在实现一个非常简单的类,它允许我在RestfullAPI上简单地执行get / post / put / delete请求。

public class NetCampusRequest {
    private String url;
    private List payload;

    public NetCampusRequest(String startUri) {
        url = startUri;
        payload = new ArrayList<NameValuePair>();
    }

    public String getUrl() {
        return url;
    }

    public List<NameValuePair> getPayload() {
        return payload;
    }

    public void addParameter(String key, String value) {
        payload.add(new BasicNameValuePair(key, value));
    }

    public void getRequest() {
        for (int i=0; i < payload.size(); i++)
            url += "?" + payload.get(i);
        System.out.println(url);
        //HttpClient client = new DefaultHttpClient();
        //HttpGet httpGet = new HttpGet()
    }
}

我在面向对象语言方面远非擅长,但似乎错误来自addParameter方法。

此外,它说有一个NullPointerException并且该应用程序工作正常,但我不认为这么多的警告是&#34;罚款&#34;。

07-03 14:20:49.760  19869-19869/fr.netcampus E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: fr.netcampus, PID: 19869
java.lang.RuntimeException: Unable to start activity ComponentInfo{fr.netcampus/fr.netcampus.DisplaySchoolsActivity}: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
        at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
        at java.util.ArrayList.get(ArrayList.java:308)
        at fr.netcampus.DisplaySchoolsActivity.onCreate(DisplaySchoolsActivity.java:57)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案