Java 如何解决android中的数组索引越界异常
调用web服务时,Java 如何解决android中的数组索引越界异常,java,android,Java,Android,调用web服务时,doInBackground(String…params)方法出错。我正在尝试编写以下代码 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add_colony_); new AddColony_LoaderTask().execute(); add_
doInBackground(String…params)
方法出错。我正在尝试编写以下代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_colony_);
new AddColony_LoaderTask().execute();
add_colony_title = (TextView) findViewById(R.id.text_colony);
note_Text=(TextView) findViewById(R.id.note_text);
btn_add_Colony_back = (ImageButton) findViewById(R.id.add_colony_back);
btnSave=(Button) findViewById(R.id.save_note);
Typeface font1 = Typeface.createFromAsset(getAssets(), "fonts/cabin.regular.ttf");
add_colony_title.setTypeface(font1); add_colony_title.setTextSize(18.0f);
btnSave.setTypeface(font1);btnSave.setTextSize(18.0f);
note_Text.setTypeface(font1);note_Text.setTextSize(15.0f);
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if (extras == null) {
bookmarkid = null;
note_type = null;
} else {
bookmarkid = extras.getString("bookmarkid");
note_type = extras.getString("note_type");
}
} else {
bookmarkid = (String) savedInstanceState.getSerializable("bookmarkid");
note_type = (String) savedInstanceState.getSerializable("note_type");
}
System.out.println(bookmarkid);
System.out.println(note_type);
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent in = new Intent(getApplicationContext(), Dashboard.class);
startActivity(in);
new AddColony_LoaderTask().execute("https://www.mysites.com/secure-mobile/note?"," access_token",bookmarkid,note_type);
}
});
}
class AddColony_LoaderTask extends AsyncTask<String, String, String>
{
protected String doInBackground(String... params) {
bookmarkid = params[2];
note_type = params[3];
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
HttpConnectionParams.setSoTimeout(httpParameters, 5000);
HttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpPost httpPost = new HttpPost(params[0]);
String jsonResult = "";
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("access_token", "94529e5dbc6234fc3bbfce7406b8dde9"));
nameValuePairs.add(new BasicNameValuePair("bookmarkId",bookmarkid));
nameValuePairs.add(new BasicNameValuePair("note",note_type));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
int status = 200;
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
System.out.println(data);
System.out.println("fffff");
JSONObject jsono = new JSONObject(data);
for (int i = 0; i < jsono.length(); i++)
{
}
return null;
}
//------------------>>
} catch (ParseException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
您只是在执行Asynchtask,没有传递任何数据
new AddColony_LoaderTask().execute();
这样做
String[] query=new String[3];
query[0]=requestURL;
query[1]=access_token;
query[2]= bookmarkid;
query[3]= note_type;
new AddColony_LoaderTask().execute(query);
检查您的
onCreate
方法
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_colony_);
new AddColony_LoaderTask().execute(); // here is the cause
add_colony_title = (TextView) findViewById(R.id.text_colony);
您正在执行任务而不传递任何参数。因此,ArrayIndexOutOfBoundsException
希望能有所帮助。你没有得到任何参数。正在尝试将数据打印到您能理解的方法。
AddColony_Activity.java:119
数据所在位置???@ELITE it giviing此行出错bookmark id=params[2];和AddColony\u Activity.java:119这是AddColony\u LoaderTask扩展的行类吗AsyncTask@Pawan检查答案,我添加了异常原因。@因此params
是一个空数组,它与错误消息的length=0
部分一致,但params[2]处出错
与index=3的错误文本不一致,因此也存在代码刷新问题。该代码不会在该行抛出该错误。这可能不是真正的问题,但也是一个问题,这就是我的评论的全部内容。获得相同的错误您是否在这些变量中传递了值?我错过了access_token参数。检查是否添加了我已更正的错误,但仍然存在相同的错误execute()
是一个varargs,因此您可以执行execute(请求URL、访问令牌、书签ID、注释类型)
。无论如何,更新的代码仍然失败,因为您仍然只分配String[3]
。如果这样做,则需要String[4]
,但varargs要容易得多。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_colony_);
new AddColony_LoaderTask().execute(); // here is the cause
add_colony_title = (TextView) findViewById(R.id.text_colony);