Android JSON Web服务有问题吗

Android JSON Web服务有问题吗,android,json,web-services,Android,Json,Web Services,我正在尝试将JSON数据获取到应用程序,但获取JSON异常 main活动 public class MainActivity extends AppCompatActivity { private ListView mListView; private List<Project> projects = new ArrayList<>(); private ProgressDialog progressDialog; @Override protected void

我正在尝试将JSON数据获取到应用程序,但获取JSON异常

main活动

public class MainActivity extends AppCompatActivity {

private ListView mListView;
private List<Project> projects = new ArrayList<>();
private ProgressDialog progressDialog;


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

    mListView = (ListView) findViewById(R.id.listView);

    progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("Loading....");
    progressDialog.setCancelable(false);

    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(MainActivity.this,Main2Activity.class);
            startActivity(intent);
        }
    });

    Button mFilterButton = (Button) findViewById(R.id.filter_button);
    mFilterButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            PopupMenu popupMenu = new PopupMenu(MainActivity.this,v);
            popupMenu.inflate(R.menu.filter_menu);
            popupMenu.show();

        }
    });

    Button mSortButton = (Button) findViewById(R.id.sort_button);
    mSortButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            PopupMenu popupMenu = new PopupMenu(MainActivity.this,v);
            popupMenu.inflate(R.menu.sort_menu);
            popupMenu.show();
        }
    });

    new GSONExecution().execute();
}


private class GSONExecution extends AsyncTask<Void, Void, Boolean>{

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog.show();
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        String urlString = "http://starlord.hackerearth.com/kickstarter";

        try {
            URL url = new URL(urlString);

            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            httpURLConnection.setRequestMethod("GET");
            int res = httpURLConnection.getResponseCode();

            if (res == 200){
                InputStream inputStream = httpURLConnection.getInputStream();
                String s = convertStreamToString(inputStream);
                Log.v("Response :" , " is "+ s);

                JSONObject rootObject = new JSONObject(s);
                JSONArray jsonArray = rootObject.getJSONArray("");

                for (int i=0; i<=jsonArray.length(); i++){
                    JSONObject contactObject = jsonArray.getJSONObject(i);

                    String titleValue = contactObject.getString("title");
                    Integer pledgedValue = contactObject.getInt("amt.pledged");
                    Integer backersValue = contactObject.getInt("num.backers");

                    Project project = new Project();
                    project.setPleadges(pledgedValue);
                    project.setBackers(backersValue);
                    project.settitle(titleValue);
                    projects.add(project);

                    Log.v("Object details : " , " : " + pledgedValue + " : " + backersValue);
                }
            }
        } catch (IOException | JSONException e) {
            e.printStackTrace();
        }
        return true;
    }

    @Override
    protected void onPostExecute(Boolean isOperationCompleted) {
        super.onPostExecute(isOperationCompleted);
        if (isOperationCompleted){
            if (progressDialog.isShowing()){
                progressDialog.dismiss();
            }
            ProjectAdapter adapter = new ProjectAdapter(MainActivity.this, projects);
            mListView.setAdapter(adapter);
        }
    }

    @NonNull
    private String convertStreamToString(InputStream inputStream) {
        StringBuilder stringBuilder = new StringBuilder();
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"),8);
            String line;
            while ((line = bufferedReader.readLine()) != null)
                stringBuilder.append(line).append("\n");
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }
}
}
public class Project {

String mtitle;
Integer mPleadges;
Integer mBackers;
String mNoDays;

public String gettitle() {
    return mtitle;
}

public void settitle(String mtitle) {
    this.mtitle = mtitle;
}

public Integer getPleadges() {
    return mPleadges;
}

public void setPleadges(Integer mPleadges) {
    this.mPleadges = mPleadges;
}

public Integer getBackers() {
    return mBackers;
}

public void setBackers(Integer mBackers) {
    this.mBackers = mBackers;
}

public String getNoDays() {
    return mNoDays;
}

public void setNoDays(String mNoDays) {
    this.mNoDays = mNoDays;
}
}
项目适配器

class ProjectAdapter extends BaseAdapter{

private List<Project> mList;
private Context mContext;

public ProjectAdapter(MainActivity mainActivity, List<Project> projects) {

    this.mList = projects;
    this.mContext = mainActivity;
}

@Override
public int getCount() {
    return mList.size();
}

@Override
public Object getItem(int position) {
    return mList.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.project_details,null,false);

    final TextView projectName = (TextView) convertView.findViewById(R.id.projectName);
    TextView pleadge = (TextView) convertView.findViewById(R.id.pledges);
    TextView backers = (TextView) convertView.findViewById(R.id.backers);

    projectName.setText(mList.get(position).gettitle());
    pleadge.setText(mList.get(position).getPleadges());
    backers.setText(mList.get(position).getBackers());

    return convertView;
}
class ProjectAdapter扩展了BaseAdapter{
私人名单;
私有上下文;
公共项目适配器(MainActivity MainActivity,列出项目){
this.mList=项目;
this.mContext=main活动;
}
@凌驾
public int getCount(){
返回mList.size();
}
@凌驾
公共对象getItem(int位置){
返回mList.get(位置);
}
@凌驾
公共长getItemId(int位置){
返回0;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutInflater充气器=(LayoutInflater)mContext.getSystemService(Context.LAYOUT\u充气器\u服务);
convertView=充气机。充气(R.布局。项目详细信息,空,假);
final TextView projectName=(TextView)convertView.findViewById(R.id.projectName);
TextView.ge=(TextView)convertView.findViewById(R.id.d);
TextView backers=(TextView)convertView.findViewById(R.id.backers);
projectName.setText(mList.get(position.gettitle());
setText(mList.get(position.getConfectes());
backers.setText(mList.get(position.getBackers());
返回视图;
}
}

我得到org.json.JSONException:Value 在org.json.json.typeMismatch(json.java:111)


我希望你们理解这个问题,我还在学习阶段,所以请给出简短的回答,以便我能理解。

你们从响应中得到
JSONArray
,并试图保持
JSONObject
,这会导致
org.json.JSONException:Value at org.json.json.typeMismatch(json.java:111)
错误

试试这个

try {
    JSONArray jsonArrayLST = new JSONArray(s);
    for (int i = 0; i < jsonArrayLST.length(); i++) {
        JSONObject contactObject= jsonArrayLST.getJSONObject(i);

        String titleValue = contactObject.getString("title");
        Integer pledgedValue = contactObject.getInt("amt.pledged");
        Integer backersValue = contactObject.getInt("num.backers");

        Project project = new Project();
        project.setPleadges(pledgedValue);
        project.setBackers(backersValue);
        project.settitle(titleValue);
        projects.add(project);

        Log.v("Object details : " , " : " + pledgedValue + " : " + backersValue);
    }
} catch (JSONException e) {
    e.printStackTrace();
}

您正在从响应中获取
JSONArray
,并试图保持
JSONObject
,这会导致
org.json.JSONException:Value位于org.json.json.typeMismatch(json.java:111)
错误

试试这个

try {
    JSONArray jsonArrayLST = new JSONArray(s);
    for (int i = 0; i < jsonArrayLST.length(); i++) {
        JSONObject contactObject= jsonArrayLST.getJSONObject(i);

        String titleValue = contactObject.getString("title");
        Integer pledgedValue = contactObject.getInt("amt.pledged");
        Integer backersValue = contactObject.getInt("num.backers");

        Project project = new Project();
        project.setPleadges(pledgedValue);
        project.setBackers(backersValue);
        project.settitle(titleValue);
        projects.add(project);

        Log.v("Object details : " , " : " + pledgedValue + " : " + backersValue);
    }
} catch (JSONException e) {
    e.printStackTrace();
}

显示json api响应看起来您没有正确解析响应。共享您的JSON响应。检查下面的回答显示您的JSON api响应看起来您没有正确解析响应。共享您的JSON响应。请检查下面的回答现在应用程序正在崩溃。获取致命异常,资源$NotFoundException位于concedge.setText(mList.get(position.getConcedges());在adapterUse-Concedge.setText(String.valueOf(mList.get(position.getConcedges()))中;您正在将int值设置为textview,这会引发错误
android.content.res.Resources$NotFoundException:String resource ID
@R.H我已对适配器进行了更改,请检查答案是的,收到了。谢天谢地,应用程序正在崩溃。获取致命异常,资源$NotFoundException位于concedge.setText(mList.get(position.getConcedges());在adapterUse-Concedge.setText(String.valueOf(mList.get(position.getConcedges()))中;您正在将int值设置为textview,这会引发错误
android.content.res.Resources$NotFoundException:String resource ID
@R.H我已对适配器进行了更改,请检查答案是的,收到了。谢谢