duanfen1992 2016-05-24 02:47
浏览 324

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

I trat of get data from a hosting, but when I execute the app in my phone, I get an error,

Error org.json.JSONException: Value< html>< body>< script of type java.lang.String cannot be converted to JSONObject Here are my variables and the code that parses the 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); 
 ?>

and this json class

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;
}

}

where is my error??

  • 写回答

1条回答 默认 最新

  • dongyan8929 2016-05-24 03:33
    关注

    It would appear that your jsonString is not what you are expecting. You might print it out, or stop in debug to inspect it. It's possible that you are getting an error message from the web server, such as 404 Not Found in a HTML page, or you have an error in your server side, or your server supports GET and not POST. Hard to say.

    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型