我试图使用PHP从mySQL数据库中获取数据.这是我实现远程获取数据的真正尝试.使用JSON. php文件运行正常,因为它在浏览器中输出为JSON字符串,并使用JSONLint对其进行了标记.所以,我不确定这里有什么问题.任何帮助将不胜...
                
我试图使用PHP从mySQL数据库中获取数据.这是我实现远程获取数据的真正尝试.使用JSON. php文件运行正常,因为它在浏览器中输出为JSON字符串,并使用JSONLint对其进行了标记.所以,我不确定这里有什么问题.任何帮助将不胜感激
这就是LogCat抛出的内容:
Error parsing data org.json.JSONException: Value <?xml of type java.lang.String cannot be converted to JSONObject
threadid=9: thread exiting with uncaught exception (group=0x401dce20)
FATAL EXCEPTION: Thread-10
java.lang.NullPointerException
at com.andaero.test.JSON.JSONMain$1.run(JSONMain.java:39)
at java.lang.Thread.run(Thread.java:1020)
更新:我从标记请求的php文件中删除了echo方法.我认为它与“JSONArray a = json.getJSONArray(”监管“)有关.我也试过其他人的方法,但没有优势.
以下是课程:
public class JSONfunctions {
    public static JSONObject getJSONfromURL(String url) {
        InputStream is = null;
        String result = "regulatory";
        JSONObject jArray = null;
        // http post
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection " + e.toString());
        }
        // convert response to string
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
        } catch (Exception e) {
            Log.e("log_tag", "Error converting result " + e.toString());
        }
        try {
            jArray = new JSONObject(result);
        } catch (JSONException e) {
            Log.e("log_tag", "Error parsing data " + e.toString());
        }
        return jArray;
    }
}
列表活动:
public class JSONMain extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);
        final ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
        new Thread(new Runnable() {
            public void run() {
                JSONObject json = JSONfunctions
                        .getJSONfromURL("http://192.168.1.34/andaero/regulatory_list_ASC.php");
                try {
                    JSONArray a = json.getJSONArray("regulatory");
                    for (int i = 0; i < a.length(); i++) {
                        HashMap<String, String> map = new HashMap<String, String>();
                        JSONObject e = a.getJSONObject(i);
                        map.put("id", String.valueOf(i));
                        map.put("label", e.getString("label"));
                        map.put("title", e.getString("title"));
                        map.put("caption", e.getString("description"));
                        map.put("dummy", e.getString("gotoURL"));
                        mylist.add(map);
                    }
                } catch (JSONException e) {
                    Log.e("log_tag", "Error parsing data " + e.toString());
                }
            }
        }).start();
        ListAdapter adapter = new SimpleAdapter(this, mylist,
                R.layout.list_item, new String[] { "label", "title", "caption",
                        "dummy" }, new int[] { R.id.label, R.id.listTitle,
                        R.id.caption, R.id.dummy });
        setListAdapter(adapter);
        final ListView lv = getListView();
        lv.setTextFilterEnabled(true);
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                @SuppressWarnings("unchecked")
                HashMap<String, String> o = (HashMap<String, String>) lv
                        .getItemAtPosition(position);
                Toast.makeText(JSONMain.this,
                        "ID '" + o.get("id") + "' was clicked.",
                        Toast.LENGTH_SHORT).show();
            }
        });
    }
} 
PHP:
<?php
//MySQL Database Connect
include 'andaerologin.php';
mysql_select_db("andaero");
$sql=mysql_query("select * from regulatory_list");
$output = array();
while($row = mysql_fetch_assoc($sql)) {
    $output['regulatory'][] = $row;
}
exit (json_encode($output));
mysql_close();
?>
解决方法:
尝试将PHP更改为:
$output = new stdClass();
$output->regulatory = array();
while($row = mysql_fetch_assoc($sql)) {
    $output->regulatory[] = $row;
}
header('Content-type: application/json');
echo (json_encode($output));
				 织梦狗教程
				
			本文标题为:java – mySQL到PHP到JSON:String无法转换为JSONObject
				
        
 
            
        基础教程推荐
             猜你喜欢
        
	     - java – 如果数据库关闭了连接,是否需要手动关闭它? 2023-11-01
 - SpringBoot中使用Servlet的两种方式小结 2023-02-26
 - Java实现单机版五子棋游戏的示例代码 2023-05-19
 - java – struts 2中的数据库访问 2023-11-01
 - MyBatis中association的基本使用方法 2023-05-24
 - @Scheduled注解不能同时执行多个定时任务的解决方案 2023-06-05
 - JSP Spring防止用户重复登录的实现方法 2023-08-01
 - 高可用架构etcd选主故障主备秒级切换实现 2022-11-03
 - 教你怎么用JSP统计网站访问人数 2023-07-30
 - Java多线程工具CompletableFuture的使用教程 2023-04-17
 
    	
    	
    	
    	
    	
    	
    	
    	
						
						
						
						
						
				
				
				
				