【问题标题】:Value Connected of type java.lang.String cannot be converted to JSONObjectjava.lang.String 类型的 Connected 值无法转换为 JSONObject
【发布时间】:2016-10-12 01:13:18
【问题描述】:

我有一个 PHP 文件,当我运行它以 JSON 格式获取所有员工时,它会从数据库中获取所有员工。

Android Studio 中的这段代码通过 php 文件从数据库中获取所有员工

当我运行应用程序时,我没有在 Android Studio 中获取数据。

它返回此错误:

java.lang.String 类型的连接值无法转换为 JSONObject

在图片中

这是 Android Studio 中的 Java 代码:

package com.example.ibrahim.samplecrud;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

public class ViewAllEmployee extends AppCompatActivity implements ListView.OnItemClickListener {

    private ListView listView;

    private String JSON_STRING;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_all_employee);
        listView = (ListView) findViewById(R.id.listView);
        listView.setOnItemClickListener(this);
        getJSON();
    }


    private void showEmployee(){
        JSONObject jsonObject = null;
        ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
        try {
            jsonObject = new JSONObject(JSON_STRING);
            JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY);

            for(int i = 0; i<result.length(); i++){
                JSONObject jo = result.getJSONObject(i);
                String id = jo.getString(Config.TAG_ID);
                String name = jo.getString(Config.TAG_NAME);

                HashMap<String,String> employees = new HashMap<>();
                employees.put(Config.TAG_ID,id);
                employees.put(Config.TAG_NAME,name);
                list.add(employees);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        ListAdapter adapter = new SimpleAdapter(
                ViewAllEmployee.this, list, R.layout.list_item,
                new String[]{Config.TAG_ID,Config.TAG_NAME},
                new int[]{R.id.id, R.id.name});

        listView.setAdapter(adapter);
    }

    private void getJSON(){
        class GetJSON extends AsyncTask<Void,Void,String>{

            ProgressDialog loading;
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loading = ProgressDialog.show(ViewAllEmployee.this,"Fetching Data","Wait...",false,false);
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                loading.dismiss();
                JSON_STRING = s;
                showEmployee();
            }

            @Override
            protected String doInBackground(Void... params) {
                RequestHandler rh = new RequestHandler();
                String s = rh.sendGetRequest(Config.URL_GET_ALL);
                return s;
            }
        }
        GetJSON gj = new GetJSON();
        gj.execute();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(this, ViewEmployee.class);
        HashMap<String,String> map =(HashMap)parent.getItemAtPosition(position);
        String empId = map.get(Config.TAG_ID).toString();
        intent.putExtra(Config.EMP_ID,empId);
        startActivity(intent);
    }
} 

【问题讨论】:

  • JSON_STRING的值是多少?
  • protected void onPostExecute(String s) { super.onPostExecute(s); loading.dismiss(); JSON_STRING = s;显示雇员(); }
  • 向我们展示JSON_STRING 的值 - 你自己检查过吗?
  • 你的字符串格式有问题 - 你应该检查你的 PHP,看看它是否输出错误。
  • 我调试 onPostExecute 方法和 JSON_STRING 方法获取值

标签: java php android json


【解决方案1】:

目前,您从服务器接收的String

Connection sucessful{"result":[{"id":2,"name":A"},{"id","pop"},...]}

它不是有效的 JSON 格式

因此您应该编辑服务器代码以返回正确的 JSON 格式,例如

{"result":[{"id":2,"name":A"},{"id","pop"},...]}

【讨论】:

  • $row['id'] , "名称"=>$row['name'] )); } //以json格式显示数组 echo json_encode(array('result'=>$result)); mysqli_close($con);
  • 只需从您的回复中删除“连接成功”
  • 你在开玩笑吗 ^__^ 我的网站花了两个多小时才找到问题出在哪里......非常感谢......祝您有美好的一天,没有代码中的错误^_^
猜你喜欢
  • 1970-01-01
  • 2017-02-03
  • 2017-10-02
  • 2013-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多