构造函数定义为采用String但方法调用显示错误'数组类型预期发现java.lang.String'

时间:2017-07-12 06:20:01

标签: java android retrofit2

我有一个注册活动,根据用户选中的复选框,对服务器进行异步调用以获取与所选字段相关的技能。我正在使用改造。我已经定义了一个SQLQuery类,其构造函数采用String参数。现在的问题是,当我使用String参数调用构造函数时,它显示错误Array type expected found java.lang.String。请有人帮我解决这个问题。

先谢谢,这是我的java文件

package com.example.vishal.internshipseekerapp;

import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

import java.util.HashSet;
import java.util.List;
import java.util.Set;


import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;



public class StudentRegistration extends AppCompatActivity implements View.OnClickListener {

    private final int numFields = 13;
    boolean[] checkField = new boolean[13];
    String[] field = {"computer vision", "content writing", "data mining", "electrical/electronics", "game development", "image processing", "marketing", "mechanical engineering", "mobile app dev", "programming", "software dev", "web dev"};
    Set<Skill> skill = new HashSet<>();

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

        ActionBar ab = getSupportActionBar();
        ab.setDisplayHomeAsUpEnabled(true);

        // register onclick listener for all checkboxes
        ( findViewById(R.id.field0)).setOnClickListener(this);
        ( findViewById(R.id.field1)).setOnClickListener(this);
        ( findViewById(R.id.field2)).setOnClickListener(this);
        ( findViewById(R.id.field3)).setOnClickListener(this);
        ( findViewById(R.id.field4)).setOnClickListener(this);
        ( findViewById(R.id.field5)).setOnClickListener(this);
        ( findViewById(R.id.field6)).setOnClickListener(this);
        ( findViewById(R.id.field7)).setOnClickListener(this);
        ( findViewById(R.id.field8)).setOnClickListener(this);
        ( findViewById(R.id.field9)).setOnClickListener(this);
        ( findViewById(R.id.field10)).setOnClickListener(this);
        ( findViewById(R.id.field11)).setOnClickListener(this);
        //( findViewById(R.id.field12)).setOnClickListener(this);

        // register onclick listener for DONE button
        Button done = (Button) findViewById(R.id.field_select_done);
        done.setOnClickListener(this);
    }

    public void onClick(View v){
        switch(v.getId()){
            case R.id.field0:
                if(((CheckBox) v).isChecked())
                    checkField[0] = true;
            case R.id.field1:
                if(((CheckBox) v).isChecked())
                    checkField[1] = true;
            case R.id.field2:
                if(((CheckBox) v).isChecked())
                    checkField[2] = true;
            case R.id.field3:
                if(((CheckBox) v).isChecked())
                    checkField[3] = true;
            case R.id.field4:
                if(((CheckBox) v).isChecked())
                    checkField[4] = true;
            case R.id.field5:
                if(((CheckBox) v).isChecked())
                    checkField[5] = true;
            case R.id.field6:
                if(((CheckBox) v).isChecked())
                    checkField[6] = true;
            case R.id.field7:
                if(((CheckBox) v).isChecked())
                    checkField[7] = true;
            case R.id.field8:
                if(((CheckBox) v).isChecked())
                    checkField[8] = true;
            case R.id.field9:
                if(((CheckBox) v).isChecked())
                    checkField[9] = true;
            case R.id.field10:
                if(((CheckBox) v).isChecked())
                    checkField[10] = true;
            case R.id.field11:
                if(((CheckBox) v).isChecked())
                    checkField[11] = true;


            case R.id.field_select_done:
                displayRelevantSkills();
        }
    }

    private void displayRelevantSkills() {
        String field = "field";
        String checkBoxName;


        final String SKILL_FIELD_URL = "https://data.outfight74.hasura-app.io/";


//        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

        // set request options for all requests
        Retrofit.Builder builder =
                new Retrofit.Builder()
                        .baseUrl(SKILL_FIELD_URL)
                        .addConverterFactory(
                                GsonConverterFactory.create()
                        );

        // create retrofit adapter
        Retrofit retrofit =
                builder
                    /*.client(
                            httpClient.build()
                    )*/
                    .build();

        // create retrofit REST client
        getRelevantSkills skillClient =  retrofit.create(getRelevantSkills.class);

        // for each checkbox do
        for(int i = 0; i < numFields; i++) {
            // if checkbox is ticked
            if(checkField[i]) {
                // fetch relevant skills from server
                SQLQuery skillQuery = new SQLQuery(field[i]);

                Call<List<Skill>> call =
                        skillClient.relevantSkills(skillQuery);

                // Execute the call asynchronously. Get a positive or negative callback.
                call.enqueue(new Callback<List<Skill>>() {
                    @Override
                    public void onResponse(Call<List<Skill>> call, Response<List<Skill>> response) {
                        // The network call was a success and we got a response
                        // add to skills HashSet
                        skill.addAll(response.body());
                        Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFailure(Call<List<Skill>> call, Throwable t) {
                        // the network call was a failure
                        // TODO: handle error
                        Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }

        // display a drop down menu having all elements of HashSet
        for(Skill s : skill)
        {
            CheckBox skillItem = new CheckBox(getApplicationContext());
            skillItem.setText(s.getSkill());
        }
    }
}

这是getRelevantSkills.java file

package com.example.vishal.internshipseekerapp;

import java.util.List;

import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.POST;


class Where{
    // this will be given by the user
    private String skill;

    public Where(String skill) {
        this.skill = skill;
    }
}

class Args{
    final String table = "skill_field_relation";
    final String[] columns = {"skill"};
    private Where where;

    public Args(String field) {
        where = new Where(field);
    }
}

class SQLQuery{
    final String type = "select";
    private Args args;
    public SQLQuery(java.lang.String field) {
        args = new Args(field);
    }
}

class Skill{
    private String skill;

    public String getSkill() {
        return skill;
    }

    public Skill(String skill) {
        this.skill = skill;
    }
}

public interface getRelevantSkills {
    @POST("/v1/query")
    Call<List<Skill>> relevantSkills(
            @Body SQLQuery fetchSkills
    );
}

1 个答案:

答案 0 :(得分:2)

您正尝试将field[i]传递给SQLQuery构造函数,但fieldString,而不是数组。您应该改为field

编辑:

private void displayRelevantSkills() {
    String field = "field";

    ...

    SQLQuery skillQuery = new SQLQuery(field[i]);
    ...
}

您有一个类型为field的本地String变量,它隐藏了同名的实例变量(其类型为String[])。

如果您打算使用实例变量(String[] field = {...};),您应该写:

SQLQuery skillQuery = new SQLQuery(this.field[i]);