Android 错误:Json解析错误:无法将java.lang.String类型的值连接转换为JSONObject

Android 错误:Json解析错误:无法将java.lang.String类型的值连接转换为JSONObject,android,json,jsonexception,Android,Json,Jsonexception,我在我的listactivity中没有得到Json响应。我尝试了所有方法,但都没有显示结果。在logcat中,我获得了成功的连接,并且可以看到数据。但是在我的listview中,它显示了此异常。 这是我的密码 public class TypeMenu extends AppCompatActivity { private String TAG = TypeMenu.class.getSimpleName(); private ProgressDialog pDialog; private

我在我的listactivity中没有得到Json响应。我尝试了所有方法,但都没有显示结果。在logcat中,我获得了成功的连接,并且可以看到数据。但是在我的listview中,它显示了此异常。 这是我的密码

public class TypeMenu extends AppCompatActivity {

private String TAG = TypeMenu.class.getSimpleName();

private ProgressDialog pDialog;
private ListView lv;

// URL to get contacts JSON
private static String url = "http://cloud.granddubai.com/brtemp/index.php";

ArrayList<HashMap<String, String>> contactList;

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

    contactList = new ArrayList<>();

    lv = (ListView) findViewById(R.id.list);

    new GetContacts().execute();
    }

  /**
    * Async task class to get json by making HTTP call
   */
    private class GetContacts extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(TypeMenu.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();
        Toast.makeText(getApplicationContext(),
                "Toast",
                Toast.LENGTH_LONG)
                .show();

      }

      @Override
      protected Void doInBackground(Void... arg0) {
        HttpHandler sh = new HttpHandler();

        // Making a request to url and getting response
          String jsonStr = sh.makeServiceCall(url);

        Log.e(TAG, "Response from url: " + jsonStr);





        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                // Getting JSON Array node
                JSONArray contacts = jsonObj.getJSONArray("menu_type");

                // looping through All Contacts
                for (int i = 0; i < contacts.length(); i++) {
                    JSONObject c = contacts.getJSONObject(i);

                    String id = c.getString("id");
                    String type = c.getString("type");
                    //String email = c.getString("email");
                  // String address = c.getString("address");
                  // String gender = c.getString("gender");

                   // Phone node is JSON Object
                   //JSONObject phone = c.getJSONObject("phone");
                   //String mobile = phone.getString("mobile");
                //   String home = phone.getString("home");
                 //  String office = phone.getString("office");

                    // tmp hash map for single contact
                    HashMap<String, String> contact = new HashMap<>();

                    // adding each child node to HashMap key => value
                    contact.put("id", id);
                    contact.put("name", type);
                   //contact.put("email", email);
                    //contact.put("mobile", mobile);

                    // adding contact to contact list
                    contactList.add(contact);


                }
                } catch (final JSONException e) {
                Log.e(TAG, "Json parsing error: " + e.getMessage());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                                "Json parsing error: " + e.getMessage(),
                                Toast.LENGTH_LONG)
                                .show();
                    }
                });

            }
           } else {
            Log.e(TAG, "Couldn't get json from server.");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(),
                            "Couldn't get json from server. Check LogCat for    possible errors!",
                            Toast.LENGTH_LONG)
                            .show();
                }
                 });

             }

        return null;
        }

       @Override
        protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (pDialog.isShowing())
            pDialog.dismiss();
        /**
         * Updating parsed JSON data into ListView
         * */
        ListAdapter adapter = new SimpleAdapter(
                TypeMenu.this, contactList,
                R.layout.list_item, new String[]{"id", "type"}, new int[]  {R.id.id,
                R.id.type});

        lv.setAdapter(adapter);
        }

   }
   }
公共类类型菜单扩展AppCompative活动{
私有字符串标记=TypeMenu.class.getSimpleName();
私人对话;
私有ListView lv;
//获取联系人JSON的URL
专用静态字符串url=”http://cloud.granddubai.com/brtemp/index.php";
ArrayList联系人列表;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity类型菜单);
contactList=新的ArrayList();
lv=(ListView)findViewById(R.id.list);
新建GetContacts().execute();
}
/**
*异步任务类通过HTTP调用获取json
*/
私有类GetContacts扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//显示进度对话框
pDialog=新建进度对话框(TypeMenu.this);
setMessage(“请稍候…”);
pDialog.setCancelable(假);
pDialog.show();
Toast.makeText(getApplicationContext(),
“敬酒”,
吐司长度(长)
.show();
}
@凌驾
受保护的Void doInBackground(Void…arg0){
HttpHandler sh=新的HttpHandler();
//向url发出请求并获得响应
字符串jsonStr=sh.makeServiceCall(url);
Log.e(标签,“来自url的响应:+jsonStr”);
if(jsonStr!=null){
试一试{
JSONObject jsonObj=新的JSONObject(jsonStr);
//获取JSON数组节点
JSONArray contacts=jsonObj.getJSONArray(“菜单类型”);
//通过所有触点循环
对于(int i=0;ivalue
联系人:put(“id”,id);
联系人。输入(“姓名”,类型);
//联系方式。放置(“电子邮件”,电子邮件);
//联系人:put(“mobile”,mobile);
//将联系人添加到联系人列表
联系人列表。添加(联系人);
}
}捕获(最终JSONException e){
Log.e(标记“Json解析错误:”+e.getMessage());
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
Toast.makeText(getApplicationContext(),
Json分析错误:“+e.getMessage(),
吐司长度(长)
.show();
}
});
}
}否则{
e(标记“无法从服务器获取json”);
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
Toast.makeText(getApplicationContext(),
“无法从服务器获取json。请检查LogCat以了解可能的错误!”,
吐司长度(长)
.show();
}
});
}
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
super.onPostExecute(结果);
//关闭进度对话框
if(pDialog.isShowing())
pDialog.disclose();
/**
*将解析的JSON数据更新到ListView中
* */
ListAdapter=新的SimpleAdapter(
TypeMenu.this,contactList,
R.layout.list_项,新字符串[]{“id”,“type”},新int[]{R.id.id,
R.id.type});
低压设置适配器(适配器);
}
}
}
这是我的logcat味精

12-15 14:25:26.092 549-962/com.example.zass.brocoli E/TypeMenu: 来自url的响应:连接 成功[{“id”:“1”,“type”:“pizza”},{“id”:“8”,“type”:“Special” 提供{“id”:“2”,“type”:“面食”},{“id”:“7”,“type”:“汤”},{“id”:“6”,“type”:“饮料”},{“id”:“5”,“type”:“早餐”},{“id”:“3”,“type”:“千层面”},{“id”:“4”,“type”:“沙拉”}] 12-15 14:25:26.092 549-962/com.example.zass.broccoli E/TypeMenu:Json解析错误:无法连接java.lang.String类型的值 将转换为JSONObject

这是我希望在listview中显示的url的输出
[{“id”:“1”,“type”:“pizza”},{“id”:“8”,“type”:“特价”},{“id”:“6”,“type”:“饮料”},{“id”:“5”,“type”:“早餐”},{“id”:“3”,“type”:“千层面”},{“id”:“4”,“type”:“沙拉”}]
这是我的json文件,它在服务器上运行良好
这是我的list_item.xml

 <LinearLayout    

     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >



    <TextView
        android:id="@+id/id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/type"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

     </LinearLayout>

编辑了一些代码,请尝试下面的代码

   try {
        JSONArray jsonArry = new JSONArray(jsonStr);


        // looping through All Contacts
        for (int i = 0; i < jsonArry.length(); i++) {
            JSONObject c = jsonArry.getJSONObject(i);

            String id = c.getString("id");
            String type = c.getString("type");

            // tmp hash map for single contact
            HashMap<String, String> contact = new HashMap<>();

            // adding each child node to HashMap key => value
            contact.put("id", id);
            contact.put("name", type);

            // adding contact to contact list
            contactList.add(contact);


        }
    } catch (final JSONException e) {
        Log.e(TAG, "Json parsing error: " + e.getMessage());
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(),
                        "Json parsing error: " + e.getMessage(),
                        Toast.LENGTH_LONG)
                        .show();
            }
        });

    }
试试看{
JSONArray jsonArry=新JSONArray(jsonStr);
//通过所有触点循环
for(int i=0;i   try {
        JSONArray jsonArry = new JSONArray(jsonStr);


        // looping through All Contacts
        for (int i = 0; i < jsonArry.length(); i++) {
            JSONObject c = jsonArry.getJSONObject(i);

            String id = c.getString("id");
            String type = c.getString("type");

            // tmp hash map for single contact
            HashMap<String, String> contact = new HashMap<>();

            // adding each child node to HashMap key => value
            contact.put("id", id);
            contact.put("name", type);

            // adding contact to contact list
            contactList.add(contact);


        }
    } catch (final JSONException e) {
        Log.e(TAG, "Json parsing error: " + e.getMessage());
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(),
                        "Json parsing error: " + e.getMessage(),
                        Toast.LENGTH_LONG)
                        .show();
            }
        });

    }
JSONArray jsonArry = new JSONArray(jsonStr);
for (int i = 0; i < jsonArry.length(); i++) {
   JSONObject c = jsonArry.getJSONObject(i);
   String id = c.getString("id");
   String type = c.getString("type");
   HashMap<String, String> contact = new HashMap<>();

   contact.put("id", id);
   contact.put("name", type);
   contactList.add(contact);
}
ListAdapter adapter = new SimpleAdapter(
            TypeMenu.this, contactList,
            R.layout.list_item, new String[]{"id", "type"}, new int[]  {R.id.id,
            R.id.type});  
ListAdapter adapter = new SimpleAdapter(
            TypeMenu.this, contactList,
            R.layout.list_item, new String[]{"id", "name"}, new int[]  {R.id.id,
            R.id.type});
<?php 
    include ('config.php');
    $id = $_GET['id'];
    $sql = mysqli_query($conn,"SELECT * FROM menu_type ");
    $response['details'] = array();
    $i=0;
    while($result = mysqli_fetch_array($sql))
    {
        $arr['id']= $result['id'];
        $arr['type']= $result['type'];
        array_push($response['details'],$arr);
    }
    echo json_encode($response);
?>
JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray detailArray = jsonObj.optJSONArray("details");
for (int i = 0; i < detailArray.length(); i++) {
    JSONObject c = detailArray.getJSONObject(i);
    String id = c.getString("id");
    String type = c.getString("type");
    HashMap<String, String> contact = new HashMap<>();

    contact.put("id", id);
    contact.put("name", type);
    contactList.add(contact);
}