【问题标题】:HTTP request with volley Java/Android使用 volley Java/Android 的 HTTP 请求
【发布时间】:2015-08-25 09:21:47
【问题描述】:

我正在尝试从页面发出 http 请求,但我的应用程序崩溃了。这是我的第一个 Java 应用程序,所以我是初学者。我研究了一段时间,但找不到解决方案:

    package com.lookingunique.splashstockcontrol;


import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;


public class MainActivity extends Activity {

EditText barcode;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    barcode = (EditText) findViewById(R.id.etbarcode);
    final Button addBtn = (Button) findViewById(R.id.button);

    addBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String barcodeval = barcode.getText().toString();
            // Instantiate the RequestQueue.
            RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
            String url ="http://xxzx.com/zzz/xzxx/yyy.php?barcode="+ barcodeval +"&action=check";

            // Request a string response from the provided URL.
            StringRequest stringRequest = new StringRequest( Request.Method.GET, url,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            // Display the first 500 characters of the response string.
                            String test = response.substring(0,100);
                            Toast.makeText(getApplicationContext(), test, Toast.LENGTH_SHORT).show();
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    String test = "That didn't work!";
                    Toast.makeText(getApplicationContext(), test, Toast.LENGTH_SHORT).show();
                }
            });
            // Add the request to the RequestQueue.
            queue.add(stringRequest);




        }
    });

    barcode.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            addBtn.setEnabled(!barcode.getText().toString().trim().isEmpty());
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
}


}

08-25 10:15:15.336 2029-2029/com.lookingunique.splashstockcontrol E/AndroidRuntime: 致命异常: main 进程:com.lookingunique.splashstockcontrol,PID:2029 java.lang.StringIndexOutOfBoundsException:长度=0;区域开始=0;区域长度=100 在 java.lang.String.startEndAndLength(String.java:298) 在 java.lang.String.substring(String.java:1087) 在 com.lookingunique.splashstockcontrol.MainActivity$1$1.onResponse(MainActivity.java:47) 在 com.lookingunique.splashstockcontrol.MainActivity$1$1.onResponse(MainActivity.java:43) 在 com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60) 在 com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30) 在 com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99) 在 android.os.Handler.handleCallback(Handler.java:739) 在 android.os.Handler.dispatchMessage(Handler.java:95) 在 android.os.Looper.loop(Looper.java:148) 在 android.app.ActivityThread.main(ActivityThread.java:5417) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

【问题讨论】:

  • 尝试将响应字符串直接放入Toast。响应并不完全符合您的预期,这是您的 substring 失败,因为原始的 String 不够大。

标签: java android http


【解决方案1】:

如果您检查文档中的子字符串,请求似乎没有失败,但响应不包含长度为 100 的字符串:

Throws: IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.

尝试只打印所有字符串

【讨论】:

  • Np 很高兴我能提供帮助