有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

php错误JSONException:类型为java的值。无法将lang.String转换为JSONObject

我试图从主机上获取数据,但当我在手机上执行应用程序时,我得到了一个错误

错误组织。json。JSONException:Value<;html>&书信电报;车身>&书信电报;java类型的脚本。lang.String无法转换为JSONObject 以下是我的变量和解析JSON文件的代码:

$host="";  
$username=""; 
$password=""; 
$db_name=""; 

$con=mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
$sql = "select * from pedidos"; 
$result = mysql_query($sql);
$json = array();

if(mysql_num_rows($result)){
while($row=mysql_fetch_assoc($result)){
$json['users1'][]=$row;
}
}
mysql_close($con);
echo json_encode($json); 
 ?>

这个json类

private String jsonResult;
private String url = "http://proyectocul.mipropia.com/jsondiego/pedidos.php";
private ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_verpedido);
    listView = (ListView) findViewById(R.id.listviewpedido);
    accessWebService();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

// Async Task to access the web
private class JsonReadTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(params[0]);
        try {
            HttpResponse response = httpclient.execute(httppost);
            jsonResult = inputStreamToString(
                    response.getEntity().getContent()).toString();
        }

        catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    private StringBuilder inputStreamToString(InputStream is) {
        String rLine = "";
        StringBuilder answer = new StringBuilder();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));

        try {
            while ((rLine = rd.readLine()) != null) {
                answer.append(rLine);
            }
        }

        catch (IOException e) {
            // e.printStackTrace();
            Toast.makeText(getApplicationContext(),
                    "Error..." + e.toString(), Toast.LENGTH_LONG).show();
        }
        return answer;
    }

    @Override
    protected void onPostExecute(String result) {
        ListDrwaer();
    }
}// end async task

public void accessWebService() {
    JsonReadTask task = new JsonReadTask();
    // passes values for the urls string array
    task.execute(new String[]{url});
}

// build hash set for list view
public void ListDrwaer() {
    List<Map<String, String>> employeeList = new ArrayList<Map<String, String>>();

    try {
        JSONObject jsonResponse = new JSONObject(jsonResult);
        JSONArray jsonMainNode = jsonResponse.optJSONArray("users1");


        for (int i = 0; i < jsonMainNode.length(); i++) {
            JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
            String name = jsonChildNode.optString("id");
            String number = jsonChildNode.optString("producto");
            String user = jsonChildNode.optString("username");
            String cantidad = jsonChildNode.optString("cantidad");
            String hora = jsonChildNode.optString("created_at");
            String outPut = "N°" + name + " - " + "Usuario:  " + user + " - " + "Producto:  " + number + " - " + "Cantidad:  " + cantidad + " - " + "Fecha:  " + hora;
            employeeList.add(createEmployee("employees", outPut));
        }
    } catch (JSONException e) {
        Toast.makeText(getApplicationContext(), "Error" + e.toString(),
                Toast.LENGTH_SHORT).show();
    }

    SimpleAdapter simpleAdapter = new SimpleAdapter(this, employeeList,
            安卓.R.layout.simple_list_item_1,
            new String[] { "employees" }, new int[] { 安卓.R.id.text1 });
    listView.setAdapter(simpleAdapter);
}

private HashMap<String, String> createEmployee(String name, String number) {
    HashMap<String, String> employeeNameNo = new HashMap<String, String>();
    employeeNameNo.put(name, number);
    return employeeNameNo;
}

}

我的错误在哪里


共 (0) 个答案