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

时间:2016-05-24 02:47:08

标签: java php android json

我从托管中获取数据,但是当我在手机中执行应用程序时,出现错误,

错误org.json.JSONException:Value< HTML><体>< java.lang.String类型的脚本无法转换为JSONObject 以下是我的变量和解析JSON-File的代码:

$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,
            android.R.layout.simple_list_item_1,
            new String[] { "employees" }, new int[] { android.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;
}

}

我的错误在哪里?

1 个答案:

答案 0 :(得分:0)

看起来您的jsonString并不是您所期望的。您可以将其打印出来,或在调试中停止检查。 您可能从Web服务器收到错误消息,例如在HTML页面中找不到404,或者您的服务器端出现错误,或者您的服务器支持GET而不是POST。很难说。